Answer on Question#39324- Programming, C++
1. Write a program that reads in characters from the user. Assume they will give you correct input, which corresponds to capital letters (A-Z) and lower-case letters (a-z). Count up the number of vowels entered versus consonants. The program will terminate when the user gives, as input, an exclamation point (!). Use a switch statement where possible.
Solution.
#include <iostream>
#include <ctype.h>
using namespace std;
int main(int argc, char* argv[])
{
int ivwl=0, icnst=0;
char ch;
char *vowels[]={"A", "E", "I", "O", "U"};
char *consonants[]={"B", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z"};
while(1)
{
cout<<">> ";
cin>>ch;
for(int i=0; i<sizeof(vowels) / sizeof(vowels[0]); i++)
if(*vowels[i]==ch) { ivwl++; }
for(int i=0; i<sizeof(consonants) / sizeof(consonants[0]); i++)
if(*consonants[i]==ch) { icnst++; }
if(ch=='!'){break;}
}
cout<<"============================;";
cout<<endl<<"Vowels: "<<ivwl<<" "<<"Consonants: "<<icnst<<endl;
cin.get();
cin.get();
return 0;
}
Comments