How to determine non-vowel in a string and display it out???

I know how to determine vowel in a string but i not sure how to change the code to make it determine the non-vowel in a string and display it out... Anyone got any idea??

#include<iostream.h>
void main()
{

char word[20];
int vowel = 0;
cout<<"Enter a name: ";
cin>>word;

for(int c=0;c<strlen(word);c++)

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

cout<<"The number of vowel is "<<vowel<<endl;
}


Last edited on
You need to be using strings to compare characters. use #include <string> and using std::string


If the letter word[c] is != a vowel, increment your counter.
But my teacher didt teach me how to use the code you mention....
You better get reading then - http://www.cprogramming.com/tutorial/string.html
word[c] is != a vowel is not working.... I try it already... it still count the vowel in the value also...
What do you mean it's not working? Post your code.
#include<iostream.h>
void main()
{

char word[20];
int vowel = 0;
cout<<"Enter a name: ";
cin>>word;

for(int c=0;c<strlen(word);c++)

{
if(word[c] != 'a' || word[c] != 'e'|| word[c] != 'i'|| word[c] != 'o'|| word[c] != 'u')
{
vowel++;
}
}

cout<<"The number of vowel is "<<vowel<<endl;
}

You mean changing it become like this???
Currently, the if statement is always entered because if it != 'a' then it will be entered, and if it is 'a', then it is obviously not 'e', so the if is entered anyway.

Look at your statement again and read it to yourself...your logic is a bit off.
Could you not approach it differently:

1
2
3
4
5
6
7
8
9
char vowels[5] = {'a', 'e', 'i', 'o', 'u'};
int i = 0, x = 0;

for(i; i < strlen(word); i++) {
     for(x; x < 5; x++) 
          if(!strcmp(word[i], vowels[x]))
             ... // output vowel
     x = 0; // set x = 0 again for the next letter to be tested
}
Topic archived. No new replies allowed.