C Program to print the number of vowels and consonants in a given string
In this article, we are going to write a program to print the number of ‘vowels’ and ‘consonants’ in a given string.
In the alphabets “a,e,i,o,u” are the vowels and the other alphabets are considered as consonants
Assume the given string is “sudhakar”
String is an array of characters. It will be stored in the memory as follows, with index from ‘0’ to ‘n-1’
s | u | d | h | a | k | a | r |
First assume, there are ‘0’ number of vowels, ‘0’ number of consonants.
Now start from the index ‘0’.
What is the first character? It is “s”, i.e. consonant. Then increment the consonant value by 1.
What is the next character? it is “u”, i.e. a vowel, so increment the vowel by 1.
What is the next character? it is “d”, i.e. a consonant, increment the consonant value by 1.
This process is continued until the end of the string. In programming the end of the string will be considered as null character that is ‘\0’.
We repeat this process in a loop until the end of the string.
Program:
Output:
Enter a String :
sudhakar
Number of Vowels in this String = 3
Number of Consonants in this String = 5
——————————–
Process exited after 3.283 seconds with return value 0
Press any key to continue . . .
In the program first a string array is declared of ‘char’ datatype. It will allow up to ’50’ characters. The variables ‘i’,’vowels’,’consonants’ are initialized with ‘0’. count from 0, if we encountered vowel or consonant in a string then increment the value of ‘vowel’ or ‘consonant’ variable by 1.
For reading a string we are using gets() function.
Condition to check the vowel or not (included upper case letters also)
if the str[i]!=’\0′, we are going to check the condition for every character in a string.
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’)
if this condition is true, means we are going to increment the count value of vowles, otherwise, we feel that the charecter is a consonant. Then in the else
case we incremented the value of consonants. we incremented the value of ‘i’ for repeating the loop till the end of the string.
Finally we are printing the number of vowels and consonants using printf statement.
The problem with this program is if we give a string “sudha kar” then the output may vary. because, it consider a white space ( between “sudha” and “kar”) as a consonant because we used only one condition in the ‘if’.
The following test case gives wrong result for the above program.
Output:
Enter a String :
sudha kar
Number of Vowels in this String = 3
Number of Consonants in this String = 6
——————————–
Process exited after 3.515 seconds with return value 0
Press any key to continue . . .
How to do this?
If encounter a space in a string, then it should be skipped. We can do it with a continue statement in “C” language.
In other words, if there is a space in a program, increment the ‘i’ value, and then continue the iteration for the next character.
Program: To count the number of vowels and consonants, including with white space character.
Test Case 1:
Output:
Enter a String :
sudha kar
Number of Vowels in this String = 3
Number of Consonants in this String = 5
——————————–
Process exited after 8.562 seconds with return value 0
Press any key to continue . . .
Test Case 2:
Output:
Enter a String :
hi sudhakar welcome
Number of Vowels in this String = 7
Number of Consonants in this String = 10
——————————–
Process exited after 11.26 seconds with return value 0
Press any key to continue . . .
Similarly, if you required you can add the conditions for special characters too…
Thank You for visiting our blog.