C program to count the total number of vowels and consonants in a string – In this article, we will discuss the multiple means to count the total number of vowels and consonants in a string in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The means mentioned in this piece are as follows:
- Using Standard Method
- Using Function
- Using Recursion
- Using Pointers and While Loop
A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.
Firstly, you need to enter a string whose vowels and consonants you need to determine.
Here, the string “hello world” is entered.
As you can see, there are:
Vowels: 3
Consonants: 7
Hence, the numbers will be printed accordingly.
Thus, the different methods to do so in C programming are as follows:
Using Standard Method
- Read the entered string and store the string into the variable ‘s’ using gets(s) function.
2) For loop iterates through string ‘s’ with the structure for(i=0;s[i];i++),
a) If the ASCII value of s[i] is in the range of 65 to 90 or 97 to 122 then check s[i] is equal to any one of the vowels(a,e,i,o,u).If s[i] is equal to any vowel then increase the vowel count, otherwise increase the consonant count.
Repeat this step up to end of the string.
3) Print the number of vowels and consonants present in the string.
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 30 |
#include <stdio.h> #include <string.h> int main() { char s[1000]; int i,vowels=0,consonants=0; printf("Enter the string : "); gets(s); for(i=0;s[i];i++) { if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 && s[i]<=122)) { if(s[i]=='a'|| s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O' ||s[i]=='U') vowels++; else consonants++; } } printf("vowels = %d\n",vowels); printf("consonants = %d\n",consonants); return 0; } |
Output:
1 2 3 |
Enter the string : hello world vowels = 3 consonants = 7 |
Using Function – Count Total No of Vowels & Consonants
- The main() calls the stringcount() function, passing the string as an argument to the function.
2) The function stringcount(char *s) counts the number of vowels and consonants present in the string.
a) The function checks the each character’s ASCII value is within the range of alphabets or not.
b) If the character is an alphabet then it compares the character with vowels. If the character is a vowel then increase the vowel count, otherwise increase the consonant count.
These a,b steps will be repeated up to end of the string using for loop with the structure for(i=0;s[i];i++).
3) The function stringcount(char *s) prints the vowels count and consonant count.
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 30 31 32 33 34 35 36 |
#include <stdio.h> #include <string.h> void stringcount(char *s) { int i,vowels=0,consonants=0; for(i=0;s[i];i++) { if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 && s[i]<=122)) { if(s[i]=='a'|| s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O' ||s[i]=='U') vowels++; else consonants++; } } printf("vowels = %d\n",vowels); printf("consonants = %d\n",consonants); } int main() { char s[1000]; printf("Enter the string: "); gets(s); stringcount(s); } |
Output:
1 2 3 |
Enter the string: goutham vowels = 3 consonants = 4 |
Using Recursion
- The main() function calls the stringcount(char *s) function by passing the string as an argument.
2) The function stringcount(char *s)
initialize vowels=0,consonants=0.
a) If s[i] is null then print the vowels count and consonants count.
b) if s[i] is not null then check s[i] is an alphabet or not, based on ASCII value.
b.1) If s[i] is an alphabet then compare s[i] with vowels if it is a vowel then increase the vowel count otherwise increase the consonant count.
b.2) i value will be increased by 1. The function calls itself.
The function calls itself recursively up to s[i] is equal to null.
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 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <stdio.h> #include <string.h> void stringcount(char *s) { static int i,vowels=0,consonants=0; if(!s[i]) { printf("vowels = %d\n",vowels); printf("consonants = %d\n",consonants); return; } else { if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 && s[i]<=122)) { if(s[i]=='a'|| s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O' ||s[i]=='U') vowels++; else consonants++; } i++; stringcount(s); } } int main() { char s[1000]; printf("Enter the string: "); gets(s); stringcount(s); } |
1 2 3 |
Enter the string: alphabets vowels = 3 consonants = 6 |
Using Pointers And While Loop
- p is the pointer variable which points the string s.
2) While loop checks first character of the string existed or not.If it has existed then check it is an alphabet or not.
a) If it has existed then check it is alphabet or not. If it is an alphabet then check it is vowel or consonant. If it is a vowel then increase the vowel count otherwise increase the consonant count.
b) Increase the p count, then pointer variable points the next character of the string. Then check the while condition for the next character of the string.
Repeat until the character at pointer variable p becomes null.
3) Print the vowels count and consonants count.
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 30 31 32 33 34 35 36 |
#include <stdio.h> #include <string.h> int main() { char s[1000],*p; int vowels=0,consonants=0; printf("Enter the string : "); gets(s); p=s; while(*p) { if( (*p>=65 && *p<=90) || (*p>=97 && *p<=122)) { if(*p=='a'|| *p=='e'||*p=='i'||*p=='o'||*p=='u'||*p=='A'||*p=='E'||*p=='I'||*p=='O' ||*p=='U') vowels++; else consonants++; } p++; } printf("vowels = %d\n",vowels); printf("consonants = %d\n",consonants); return 0; } |
1 2 3 |
Enter the string : AEIOU aeiou vowels = 10 consonants = 0 |