In: Computer Science
I need a C++ program using while loops that counts the number of characters in a sentence. The user inputs a sentence and then terminates the input with either '.' or '!'. And then it needs to count and display the number of a's, e's, i's, o's, u's, and consonants. The program should read both lower and upper case. They don't want us using switch statements or string operators, and want us to us if else if statements. I have the main while loop that counts the total number of characters in a sentence, but can't get the program to count the individual vowels and the consonants. Could you comment the steps so I can figure out what I'm missing?
I WROTE THE CODE ALONG WITH THE COMMENTS:
CODE:
#include <iostream>
using namespace std;
int main()
{
//variables declaration.
char sentence[1000],ch;
int
i=0,count_char=0,count_a=0,count_e=0,count_i=0,count_o=0,count_u=0,count_consonants=0;
cout<<"Enter sentence: ";
while(ch!='!' && ch!='.') //condition checking.
{
cin>>ch; //scan one by one character.
sentence[i]=ch;
i++;
if((ch>='a'&& ch<='z') || (ch>='A' &&
ch<='Z')) //condition for character
{
if(ch=='a' || ch=='A') //condition for a and A.
count_a++;
else if(ch=='e' || ch=='E') //condition for e or E.
count_e++;
else if(ch=='i' || ch=='I') //condition for i or I.
count_i++;
else if(ch=='o' || ch=='O') //condition for o or O.
count_o++;
else if(ch=='u' || ch=='U') //condition for u or U.
count_u++;
else //condition for consonants.
count_consonants++;
count_char++;
}
}
//display results.
cout<<"Number of characters:
"<<count_char<<endl;
cout<<"Number of a's: "<<count_a<<endl;
cout<<"Number of e's: "<<count_e<<endl;
cout<<"Number of i's: "<<count_i<<endl;
cout<<"Number of o's: "<<count_o<<endl;
cout<<"Number of u's: "<<count_u<<endl;
cout<<"Number of consonants:
"<<count_consonants<<endl;
return 0;
}
OUTPUT:
SCREENSHOT OF THE CODE: