Hi all there seems to be an error in this program in the sense it is showing wrong output. Rather than showing number of consonants and vowels it is printing the length of the string.
#include<iostream>
using namespace std; // program to display the vowels and consonants of a string
int countvowels(char a[])
{
int count =0;
for(int i =0; a[i]!='\0';i++)
{
if((a[i]>=65 && a[i]<=90) || (a[i]>=97 && a[i]<=122)) // ignores digits,special characters
{
if((a[i]=65) || (a[i]=69) || (a[i]=73) || (a[i]=79) || (a[i]=85) || (a[i]=97) || (a[i]=101) || (a[i]=105) || (a[i]=111) || (a[i]=117) ) //ignores consonants
{
count++; //counts filtered out vowels
}
}
}
return count; // returns number of vowels which will be captured by x of main function
}
int countconsonants(char a[])
{
int count =0;
for(int i =0; a[i]!='\0';i++)
{
if((a[i]>=65 && a[i]<=90) || (a[i]>=97 && a[i]<=122)) // ignores digits,special characters
{
if((a[i]!=65) || (a[i]!=69) || (a[i]!=73) || (a[i]!=79) || (a[i]!=85) || (a[i]!=97) || (a[i]!=101) || (a[i]!=105) || (a[i]!=111) ||(a[i]!=117) ) //ignores vowels
{
count++; //counts filtered out consonants
}
}
}
return count; // returns number of consonants which will be captured by y of
main function
}
int main()
{
char a[100];
cout<<"Enter the string"<<endl;cin.get(a,100);
int x = countvowels(a);
int y = countconsonants(a);
cout<<"Number of vowels is"<<x<<endl; //nothing much to say about this part of the program. x just displays it and y does the same
cout<<"Number of consonants is"<<y<<endl;
return 0;
}
Here are the ASCII values of vowels.
- A , a = 65,97
- E,e = 69, 101
- I,i = 73, 105
- O,o = 79,111
- U,u = 85,117
g++ -Wall -Wextra -g
with GCC...), improve it to get no warnings, and use the debuggergdb
(and perhaps other tools, e.g.valgrind
). – Basile Starynkevitch Dec 3 '17 at 8:36=
instead of==
in one of your if statements, for one thing – ThunderStruct Dec 3 '17 at 8:39'a'
, not 97. Look also some C++ reference. Consider isalpha – Basile Starynkevitch Dec 3 '17 at 8:40