How to count the vowels in a word?

Good day!

I'm in desperate need for help. No one I know understands C++. I've searched the web for whatever useful info but ended up with finding tips for integers.

I would like to create a program that fulfills these design specifications:

Input: Any alphanumeric "word".

Output:
If there is an even number of vowels, the actual number will be displayed. (e.g. Number of vowels: 3)
If there is an odd number of vowels, the number of consonants and numbers will be displayed. (NOT vowels) (e.g. Number of Non-Vowels: 7)

I'm quite confused about how the compiler can check each letter in the input word.
I've tried equating the string that contains the input and the string vowel="aeiou" but I can't get the program correctly. Please help!
Last edited on
I've tried equating the string that contains the input and the string vowel="aeiou"


That simply checks to see if the string is "aeiou". It doesn't count the vowels in the string.

Have you learned about for loops yet?

Step through the string one character at a time and check each individual character. You can then count how many vowels there are.
Thank you very much for checking my post! I'm very grateful! I'm just new to programming.

I used the for-statement loop to make all the inputs lowercase so when I input any case of exit it would exit the program. I've tried using the same technique with finding the vowels with a for loop but I ended up getting bugs.

With my online tutorial knowledge, I've done this.
This is my current code with many missing parts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 
#include <iostream>
#include <string>
#include <cctype>


using namespace std;

string vowel="aeiou";
string conso="bcdfghjklmnpqrstvwxyz";
string word;

int main()

{
   
     do
    {
    cout << "Enter an Alphanumeric word!" << endl;
    
    cin >> word;
    
   
        
    for (int i=0; word [i] != '\0'; i++)
            { word[i] = tolower (word[i]); }
    
   
    } while (word != "exit");
 
 
    system ("pause");
    return 0;
    }


Last edited on
I tried this but I'm not sure how to count the vowels and also the consonants.
I tried the for-loop so when the letter is not null it will continue to the next letter in the word. Whenever the letter in the word[i] is the same as any letter in the vowel string, the number will add 1 and same goes for the consonants.
It didn't work unfortunately.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 #include <iostream>
#include <string>
#include <cctype>

using namespace std;

string vowel="aeiou";
string conso="bcdfghjklmnpqrstvwxyz";
string word, number, numbera;

int main()

{
   
     do
    {
    cout << "Enter an Alphanumeric word!" << endl;
    
    cin >> word;
    
    for (int i=0; word [i] != '\0'; i++)
            { word[i] = tolower (word[i]); }
    
    for (int i=0, word [i] != '\0'; i++)
      {
 If (word[i]== vowel[i] ) { number == number +1}
          else if (word(word[i]== conso[i] ) { numbera == numbera +1}
}
cout << number << endl;
cout << numbera << endl;

    } while (word != "exit");
 
    system ("pause");
    return 0;
    }


I don't know how to check for whether there are even nor odd number of vowels.
Last edited on
You can use the find function of string to see if it finds the current character in vowel or conso
http://www.cplusplus.com/reference/string/string/find/


something like this:

1
2
3
4
5
6
7
8
9
10
11
// if it finds the character inside conso
if ((conso.find(word[i])>= 0) && (conso.find(word[i])< conso.length())) 
{ 
  number = number +1;  // use a single = and not == 
}
// if it finds the character inside  vowel
else if ((vowel.find(word[i])>= 0) && (vowel.find(word[i])< vowel.length())) 
{ 
  numbera = numbera +1;
}
Last edited on
I'm not sure how to effectively use find but I wrote this code while figuring things out. Kindly help me with it.
The program does not run unfortunately. It tells me that there's something wrong in the if (word[i]="a") num = num + 1;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <cctype>


using namespace std;

string vowel="aeiou";
string conso="bcdfghjklmnpqrstvwxyz";
string word;
int num(0);

int main()

{
   
     do
    {
    cout << "Enter an Alphanumeric word!" << endl;
    
    cin >> word;
    
   
        
    for (int i=0; word [i] != '\0'; i++)
            { word[i] = tolower (word[i]); }
    for (int i=0; word [i] != '\0'; i++)
     { if (word[i]="a") num = num + 1;
     else if (word[i]="a") num = num + 1;
     else if (word[i]="e") num = num + 1;
     else if (word[i]="i") num = num + 1;
     else if (word[i]="o") num = num + 1;
     else if (word[i]="u") num = num + 1; }
     
     cout << num << endl;
     
    ;
    
    cout << num << endl;
    
     
    
   
    
    } while (word != "exit");
 
 
    system ("pause");
    return 0;
    }


I'll try doing the find since it would help me in future programming. I feel like a caveman with computers...
Last edited on
word[i]="a" is wrong because word[i] is a single character and "a" a string. Also a simple = sets the value.

if you wonna compare it, use word[i]=='a'


Checking each letter should work, but if you want to do the same with all letters of conso it will be much code.
Or there are only letters allowed as input (no numbers or special signs)?
Last edited on
Any input will do. signs symbols letters

Thank you very much for helping me out here. :-)

I tried using the find but the compiler tells me that there's something wrong with the line "number = number +1 "
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
#include <cctype>


using namespace std;

string vowel="aeiou";
string conso="bcdfghjklmnpqrstvwxyz";
string word, number, numbera;


int main()

{
   
     do
    {
    cout << "Enter an Alphanumeric word!" << endl;
    
    cin >> word;
    
   
        
    for (int i=0; word [i] != '\0'; i++)
            { word[i] = tolower (word[i]); }
    
     
    
    
    for (int i=0; word [i] != '\0'; i++)
    {
if ((conso.find(word[i])>= 0) && (conso.find(word[i])< conso.length())) 
{ 
  number = number +1;  
}

else if ((vowel.find(word[i])>= 0) && (vowel.find(word[i])< vowel.length())) 
{ 
  numbera = numbera +1;
}
    
}
    
    
    
    
    
    cout << num << endl;
    
     
    
   
    
    } while (word != "exit");
 
 
    system ("pause");
    return 0;
    }


Last edited on
because you use strig for number and numbera.
string word, number, numbera;

use int like
1
2
string word;
int number=0, numbera=0;

YAHOOO it finally worked!

Thanks for all the help!!!

I will never forget your kindness! :-)

I think I get what you mean...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;

int main() {

char *string;
string = new char[];

cout << "input the word..." << endl;
cin >> string;

for (int i=0, vowel=0, nonVowel=0;string[i]!='\0';i++) {

if (string[i]=='a' || string[i]=='e' || string[i]=='i' || string[i]=='o' || string[i]=='u') {
vowel++;


if (string[i]!='a' || string[i]!='e' || string[i]!='i' || string[i]!='o' || string[i]!='u') {
nonVowel++;
}
}
if (string[i+1] == '\0' && vowel%2 == 0) {
cout << "The number of vowels is: " << vowel;


if (string[i+1] == '\0' && vowel%2 == 1) {
cout << "The number of non-vowels is: " << nonVowel;
}
}

}

}



I just did this code so I didn't test it and it is messy, but you can improve it by using functions...


EDIT: In my opinion, whenever it is possible (I beleive it's always possible) use char[] arrays rather than strings.
Last edited on
The way you did don't works correctly
1
2
3
4
5
6
7
8
if (string[i]=='a' || string[i]=='e' || string[i]=='i' || string[i]=='o' || string[i]=='u') 
{
  vowel++;
  if (string[i]!='a' || string[i]!='e' || string[i]!='i' || string[i]!='o' || string[i]!='u') 
  {
    nonVowel++;
  }
}


for example if string[i] is an 'e', the first if-condition is true (false || true || false .. = true).
So it increases vowel by 1.
After this als the second if-condition is true, because 'e' is not an 'a', not an 'i' and so on
(true || false || true || true || true = true).
So it also counds 'e' as a nonVowel.

of course if it would work with if-else
1
2
3
4
5
6
7
8
if (string[i]=='a' || string[i]=='e' || string[i]=='i' || string[i]=='o' || string[i]=='u') 
{
  vowel++;
}
else
{
  nonVowel++;
}


but a non-vowel isn't also a consonant each time (like '2', '.' or ' '). And if you wonna get their count with this way, it's a lot of code

if (string[i]=='b' || string[i]=='c' || string[i]=='d' || string[i]=='f' || ......... )

The only thing to do it your way is to pass this with an extra check like if(string[i] >='a' && string[i]<='z')
Last edited on
With your help, I've come up with this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <cctype>


using namespace std;

string vowel="aeiou";
string conso="bcdfghjklmnpqrstvwxyz";
string word;
int number=0, numbera=0;


int main()

{
   
do
{
    cout << "Enter the Dragon! Haha! Enter a word!" << endl;
    
    cin >> word;
 
    for (int i=0; word [i] != '\0'; i++)
    { word[i] = tolower (word[i]); }
  
    for (int i=0; word [i] != '\0'; i++)
    {
         if ((conso.find(word[i])>= 0) && (conso.find(word[i])< conso.length())) 
            { number = number +1;  }

         else if ((vowel.find(word[i])>= 0) && (vowel.find(word[i])< vowel.length())) 
                 { numbera = numbera +1;}

    }
    
     if ( numbera%2 != 0 )
    cout << "The number of consonants is: " << number << endl;
    
    else if (numbera == 0)
    cout << "The number of vowels is:" << "0" << endl;
    
    else if ( numbera%2 == 0)
    cout << "The number of vowels is: " << numbera << endl;
    } while (word != "exit");
 
system ("pause");
 return 0;
 }


This code seems to add the number of vowels and consonants.

Example:
I input: haa

output: number of vowels is:2

I input another word: haa

output: number of vowels is:4

I'm not sure how to revert the numbers back to their initial value which is 0.



You can reset them to 0 at the begin of do-while-loop, like:

1
2
3
4
5
6
7
...
do
{
    number=0;
    numbera=0;
    cout << "Enter the Dragon! Haha! Enter a word!" << endl;
...
Taking my code as an example, yes you could use else, or change the if else statement from || (or's) to &&(ands)
EDIT: In my opinion, whenever it is possible (I beleive it's always possible) use char[] arrays rather than strings.


I disagree strongly.

I'm not std::string's biggest fan (I actually dislike a lot of things about it), but it's a lot better/safer than char arrays for casual string manipulation.
Agree with Disch, c-style strings are antiquated. They can be useful but in general the string class is more reliable.
Topic archived. No new replies allowed.