# Must count the number of vowels in a string.

Page 1 of 1

## 10 Replies - 15523 Views - Last Post: 14 October 2011 - 05:53 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=251090&amp;s=1945b3fcdbb6ad1ec88d9bf73e28e0a0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ridgehype23

Reputation: 0
• Posts: 25
• Joined: 28-September 11

# Must count the number of vowels in a string.

Posted 12 October 2011 - 11:26 PM

Using the function int count_string(string str) write a program that counts the number of vowels in tat string.

This is the code I have:

```#include <iostream>
#include <string>

using namespace std;

int vowels = 0;

int count_vowel(string str)
{

if (str == "a") vowels++;
if (str == "e") vowels++;
if (str == "i") vowels++;
if (str == "o") vowels++;
if (str == "u") vowels++;
if (str == "A") vowels++;
if (str == "E") vowels++;
if (str == "I") vowels++;
if (str == "O") vowels++;
if (str == "U") vowels++;
return 0;
}

int main(void) {
string input;
while ( true ) {
cout << "Enter a string or Q to quit: ";
getline( cin, input );
count_vowel ( input);
if ( input == "Q" )
break;
cout << " This string has " << vowels << " vowels!";
}
return 0;
}
```

I don't know how to make it count the number of vowels.

Is This A Good Question/Topic? 0

## Replies To: Must count the number of vowels in a string.

### #2 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• Joined: 24-July 11

## Re: Must count the number of vowels in a string.

Posted 12 October 2011 - 11:36 PM

What about you remove each instance of the vowel and them count the difference in characters between the original string and the edited string

J-Bo

### #3 jimblumberg

Reputation: 4652
• Posts: 14,584
• Joined: 25-December 09

## Re: Must count the number of vowels in a string.

Posted 13 October 2011 - 06:36 AM

Quote

I don't know how to make it count the number of vowels.

In your count_vowel() function you are testing if the vowel is equal to the string, not if the vowel is contained in the string. You will need to check the string character by character to see if that character is a vowel.

Also you should stop using the global variable. Use your return statement to return the number of vowels that are contained in the string.

Jim

This post has been edited by jimblumberg: 13 October 2011 - 06:36 AM

### #4 ridgehype23

Reputation: 0
• Posts: 25
• Joined: 28-September 11

## Re: Must count the number of vowels in a string.

Posted 13 October 2011 - 05:09 PM

This is the new code I have

```#include <iostream>
#include <string>

using namespace std;

int vowels = 0;

int count_vowel(string str)
{

if (str = "a") return vowels++;
if (str = "e") return vowels++;
if (str = "i") return vowels++;
if (str = "o") return vowels++;
if (str = "u") return vowels++;
if (str = "A") return vowels++;
if (str = "E") return vowels++;
if (str = "I") return vowels++;
if (str = "O") return vowels++;
if (str = "U") return vowels++;
return 0;
}

int main(void) {
string input;
while ( true ) {
cout << "Enter a string or Q to quit: ";
getline( cin, input );
count_vowel ( input);
if ( input == "Q" )
break;
cout << " This string has " << vowels << " vowels!";
}
return 0;
}
```

### #5 Ap0C552

• D.I.C Regular

Reputation: -6
• Posts: 321
• Joined: 08-December 10

## Re: Must count the number of vowels in a string.

Posted 13 October 2011 - 06:22 PM

First you are not counting specific characters in the string. You are check if the whole string is equal to that specific vowel.

If the input string is "test" you are asking is "test"==a?

Secondly inside the conditional of your if statement you are assigning that specific value to that variable, which is always true. You need double "==" like in the first iteration of your code.

strings are arrays of characters. You need to individually access each element in the char array and check if it is a vowel. You are familiar with arrays right?

You need to do this in a loop.

This post has been edited by Ap0C552: 13 October 2011 - 06:24 PM

### #6 ridgehype23

Reputation: 0
• Posts: 25
• Joined: 28-September 11

## Re: Must count the number of vowels in a string.

Posted 13 October 2011 - 06:29 PM

So use the double equal signs? and no Im not, but I can do some research on them.

Ap0C552, on 13 October 2011 - 06:22 PM, said:

First you are not counting specific characters in the string. You are check if the whole string is equal to that specific vowel.

If the input string is "test" you are asking is "test"==a?

Secondly inside the conditional of your if statement you are assigning that specific value to that variable, which is always true. You need double "==" like in the first iteration of your code.

strings are arrays of characters. You need to individually access each element in the char array and check if it is a vowel. You are familiar with arrays right?

You need to do this in a loop.

### #7 Ap0C552

• D.I.C Regular

Reputation: -6
• Posts: 321
• Joined: 08-December 10

## Re: Must count the number of vowels in a string.

Posted 13 October 2011 - 06:37 PM

yes use double ==

```
//here is a char names str

char str;

//here is a string named str denoted by []

char str[]="test";

//not String str

```

t is at position 0 in the array
e is at position 1 in the array
s is at position 2 in the array
t is at position 3 in the array

so to check if the first letter is a vowel we would go

```
if(str[0]=='A')vowels++;
if(str[0]=='E')vowels++;
if(str[0]=='I')vowels++;
if(str[0]=='O')vowels++;
if(str[0]=='U')vowels++;
if(str[0]=='a')vowels++;
if(str[0]=='e')vowels++;
if(str[0]=='i')vowels++;
if(str[0]=='o')vowels++;
if(str[0]=='u')vowels++;

```

This post has been edited by Ap0C552: 13 October 2011 - 06:39 PM

### #8 ridgehype23

Reputation: 0
• Posts: 25
• Joined: 28-September 11

## Re: Must count the number of vowels in a string.

Posted 13 October 2011 - 06:40 PM

How would you check all of the letters?

### #9 Ap0C552

• D.I.C Regular

Reputation: -6
• Posts: 321
• Joined: 08-December 10

## Re: Must count the number of vowels in a string.

Posted 13 October 2011 - 06:45 PM

Using a "for loop", are you familiar with loops?

Have you been skipping class? LOL

This post has been edited by Ap0C552: 13 October 2011 - 06:45 PM

### #10 Gungnir

Reputation: 152
• Posts: 527
• Joined: 21-May 11

## Re: Must count the number of vowels in a string.

Posted 14 October 2011 - 05:48 AM

My FOR loops tutorial

Try that and see if it helps.

A character array and conditional loop is the most simple solution to this problem, so it's probably your best bet.

Hope this helped!

### #11 Gungnir

Reputation: 152
• Posts: 527
• Joined: 21-May 11

## Re: Must count the number of vowels in a string.

Posted 14 October 2011 - 05:53 AM

EDIT: Also note that your count_vowels function always returns 0 because you hard coded it like that.

It should return vowels and vowels should be in the local scope of the function, otherwise if you tested two different words it would count the vowels in both instead of resetting to 0.
```
int count_vowel(string str)
{
int vowels = 0;

//for(the loop to check if there is a vowel)
//{
//    if(the scanned letter is a vowel)
//    {
//        vowels++;
//    }
//}
return vowels;
}

```

It's a simple fix, but it should save you a minor headache in debugging.

Hope this helped, too!

This post has been edited by Gungnir: 14 October 2011 - 06:00 AM