Program to count vowels, consonants, etc.
Output
Enter a line of string: C++ 20 is the latest version of C++ yet. Vowels: 9 Consonants: 16 Digits: 2 White spaces: 8
Here, the string entered by the user is stored in the line variable.
Initially, the variables vowel, consonant, digit, and space are initialized to 0.
Then, a for loop is used to iterate over the characters of the string. In each iteration, we:
- convert the character to lowercase using the
tolower()function - check whether the character is a vowel, a consonant, a digit, or an empty space. Suppose the character is a consonant. Then, the
consonantvariable is increased by 1.
When the loop ends, the number of vowels, consonants, digits, and white spaces are stored in variables vowel, consonant, digit, and space respectively.
Note: We have used the tolower() function to simplify our program. To use this function, we need to import the ctype.h header file.