C++ Program to Count the Number of Vowels/Consonants/Numbers/Special Characters in a String

This is is a C++ Program to Count the Number of Vowels/Consonants/Numbers/Special Characters in a String.

Problem Description

The program takes a string and counts the number of vowels, consonants, numbers and special characters in the string and prints them.

Problem Solution

1. The program takes a string.
2. Using if else condition, the string is checked for vowels, consonants, numbers and special characters and counted.
3. The result is printed.
4. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Count the Number of Vowels/Consonants/Numbers/Special Characters in a String. The program output is shown below.

  1. #include<iostream>
  2. #include<string.h>
  3. using namespace std;
  4. int main ()
  5. {
  6.     char str[50];
  7.     int v = 0, c = 0, n = 0, s = 0;
  8.     cout << "Enter a string : ";
  9.     gets(str);
  10.     for (int i = 0; str[i]!='\0'; ++i)
  11.     {
  12.         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')
  13.             ++v;
  14.         else if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
  15.                 ++c;
  16.              else if (str[i] >= '0' && str[i] <= '9')
  17.                       ++n;
  18.                   else
  19.                       ++s;
  20.     }
  21.     cout << "Number of vowels : " << v;
  22.     cout << "\nNumber of consonants : " << c;
  23.     cout << "\nNumber of numbers :" << n;
  24.     cout << "\nNumber of special characters : " << s;
  25.     return 0;
  26. }
Program Explanation

1. The user is asked to enter a string. Using a string function it is stored in ‘str’.
2. Variables ‘v’, ‘c’, ‘n’, ‘s’ are initialized to count vowels, consonants, numbers and special characters respectively.
3. Using nested if else condition, if any of the characters are encountered, their respective variables are incremented.
4. The result is then printed.

advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter a string : Welcome to the world!
Number of vowels : 6
Number of consonants : 11
Number of numbers :0
Number of special characters : 4
 
Case 2 :
Enter a string : $@nT@ (L@u& 24
Number of vowels : 1
Number of consonants : 3
Number of numbers :2
Number of special characters : 8
 
Case 3 :
Enter a string : 63M5/888s
Number of vowels : 0
Number of consonants : 2
Number of numbers :6
Number of special characters : 1

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.