In this tutorial you will learn about the C++ Program to Find the Number of Vowels, Consonants, Digits and White Spaces in a String and its application with practical example.
In this tutorial, we will learn to create a C++ program that will Find the Number of Vowels, Consonants, Digits, and White Spaces in a String in C programming
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:
- Operators in C++ Programming.
- Basic Input and Output function in C++ Programming.
- Basic C++ programming.
- For loop in C++ programming.
About The Program
The string is a collection of numbers, vowels, consonants, and white spaces. In today’s program, we will count the vowels, consonants, digits, and white spaces from the input string. For this, we will pass that string to multiple conditions and loop to remove the vowels, consonants, Digits, and blank spaces.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the variables for the program. 2. Taking the input number from the user. 3. Adding the given sting to program . 4. Passing those variables to forloop. 5. Using those numbers to be saved in variables and filter them with for loop. 6. Printing the result words. |
Program to count Number of Vowels, Consonants, Digits and White Spaces:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/* C++ Program to Program to Find the Number of Vowels, Consonants, Digits and White Spaces in a String */ #include <iostream> using namespace std; int main() { char str[] = {"Environment W3ADDA"}; int vowels, consonants, digits, spaces; vowels = consonants = digits = spaces = 0; 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') { ++vowels; } else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z')) { ++consonants; } else if(str[i]>='0' && str[i]<='9') { ++digits; } else if (str[i]==' ') { ++spaces; } } cout << "The string is: " << str << endl; cout << "Vowels: " << vowels << endl; cout << "Consonants: " << consonants << endl; cout << "Digits: " << digits << endl; cout << "White spaces: " << spaces << endl; return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- str[] = it will hold the string value.
- vowels = it will hold the integer value for vowels.
- consonant = it will hold the integer value for consonants.
- digits = it will hold the integer value for numbers.
- spaces = it will hold the integer value for spaces.
Input strings from the user.
Calculating the Number of Vowels, Consonants, Digits, and White Spaces in a String.
Printing output.