Given a string and write a C program to count the number of vowels and consonants in this string.
Examples:
Input: str = "geeks for geeks" Output: Vowels: 5 Consonants: 8 Input: str = "abcdefghijklmnopqrstuvwxyz" Output: Vowels: 5 Consonants: 21
Using For Loops
- Take the string as input
- Take each character from this string to check
- If this character is a vowel, increment the count of vowels
- Else increment the count of consonants.
- Print the total count of vowels and consonants in the end.
Below is the implementation of the above approach:
C
// C program to count the number of // vowels and consonants in a string #include <stdio.h> // Function to count number // of vowels and consonant void count_vowels_and_consonant( char * str)
{ // Declare the variable vowels and consonant
int vowels = 0, consonants = 0;
int i;
char ch;
// Take each character from this string to check
for (i = 0; str[i] != '\0' ; i++) {
ch = str[i];
// If this character is a vowel,
// increment the count of vowels
if (ch == 'a' || ch == 'e'
|| ch == 'i' || ch == 'o'
|| ch == 'u' || ch == 'A'
|| ch == 'E' || ch == 'I'
|| ch == 'O' || ch == 'U' )
vowels++;
// If this character is a space
// skip it
else if (ch == ' ' )
continue ;
else
// Else increment the count of consonants
consonants++;
}
// Print the total count of vowels and consonants
printf ( "\nVowels: %d" , vowels);
printf ( "\nConsonants: %d" , consonants);
} // Driver function. int main()
{ char * str = "geeks for geeks" ;
printf ( "String: %s" , str);
count_vowels_and_consonant(str);
return 0;
} |
Output
String: geeks for geeks Vowels: 5 Consonants: 8
The complexity of the above method
Time Complexity: O(1)
Auxiliary Space: O(1)
Using Recursion
Below is the C program count number of vowels and consonants in a String using recursion:
C
// C program to count the number of // vowels and consonants in a string #include <stdio.h> #include <string.h> // Function to count vowels and consonants void stringcount( char * s)
{ static int i, vowels = 0, consonants = 0;
if (!s[i]) {
printf ( "\nVowels: %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);
}
} // Driver code int main()
{ char s[1000] = "Geeks for Geeks" ;
printf ( "String: %s" , s);
stringcount(s);
} |
Output
String: Geeks for Geeks Vowels: 5 Consonants: 8
Using Pointers and While Loop
Below is the C program to count the number of vowels and consonants in a string using pointers and while loop:
C
// C program to count the number of // vowels and consonants in a string #include <stdio.h> #include <string.h> // Driver code int main()
{ char s[1000] = "geeks for geeks" , *p;
int vowels = 0, consonants = 0;
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 ( "String: %s" , s);
printf ( "\nVowels: %d\n" , vowels);
printf ( "Consonants: %d\n" , consonants);
return 0;
} |
Output
String: geeks for geeks Vowels: 5 Consonants: 8