Answer to Question #197019 in C++ for SWB

Question #197019

•Write a program that asks the user to enter the string using getline function. It then calculates the number of vowels and the number of consonants in the string

–Hint :Remember that spaces or other non alpha characters should be ignored

–Any function that does it?


1
Expert's answer
2021-05-23T14:37:46-0400
#include <iostream>
using namespace std;
int main(){
   char str[100];
   int vowelCounter = 0, consonantCounter = 0;
   cout << "Enter any string: ";
   cin.getline(str, 150);
   //'\0 represent end of string
   for(int i = 0; str[i]!='\0'; i++) {
      if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||
         str[i]=='o' || str[i]=='u' || str[i]=='A' ||
         str[i]=='E' || str[i]=='I' || str[i]=='O' ||
         str[i]=='U')
      {
         vowelCounter++;
      }
      else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
      {
         consonantCounter++;
      }
   }
   cout << "Vowels in String: " << vowelCounter << endl;
   cout << "Consonants in String: " << consonantCounter << endl;
   return 0;
}


OUTPUT:


Enter any string:It is those who concentrate on but one thing at a time who advance in this world. 
Vowels in String: 25
Consonants in String: 40

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog