Example:
Input text: I love CodeforWin!
Total Vowels = 7
Total Consonants = 8
Required knowledge
Basic C programming, If else, For loop, StringLogic to count number of vowels and consonants
Logic to count total number of vowels/consonants uses a basic concept. What we need to do is we need to traverse entire string character by character and increment the vowel counter if the current character is vowel otherwise increment consonant counter. Therefore before counting total number of vowels and consonants in a string we must know how to check whether the current character is vowel or consonant.Algorithm to count total number of vowels and consonants in a string %%Input : text {Array of characters /String} N {Size of the string} Begin: vowel ← 0; consonant ← 0; For i ← 0 to N do If (text[i] == 'a','e','i','o','u','A','E','I','O','U') then vowel ← vowel + 1; End if Else if (text[i] >='a' and <='z') or (text[i] >='A' and <='Z') then consonant ← consonant + 1; End if End for End
There are various ways you can do this program. I am writing two of them.
Program to count number of vowels and consonants
/** * C program to count total number of vowel or consonant in a string */ #include <stdio.h> #include <string.h> #define MAX_SIZE 100 //Maximum size of the string int main() { char string[MAX_SIZE]; int i, len, vowel, consonant; /* Reads string from user */ printf("Enter any string: "); gets(string); vowel = 0; consonant = 0; len = strlen(string); for(i=0; i<len; i++) { /* * If the current character(string[i]) is a vowel both upper and lowercase characters */ if(string[i] =='a' || string[i]=='e' || string[i]=='i' || string[i]=='o' || string[i]=='u' || string[i]=='A' || string[i]=='E' || string[i]=='I' || string[i]=='O' || string[i]=='U') { vowel++; } else if((string[i]>='a' && string[i]<='z') || (string[i]>='A' && string[i]<='Z')) { consonant++; } } printf("Total number of vowel = %d\n", vowel); printf("Total number of consonant = %d\n", consonant); return 0; }
Program to count number of vowels and consonants using switch
/** * C program to count total number of vowel or consonant in a string using switch case */ #include <stdio.h> #include <string.h> #define MAX_SIZE 100 //Maximum size of the string int main() { char string[MAX_SIZE]; int i, len, vowel, consonant; /* Reads a strings from user */ printf("Enter any string: "); gets(string); vowel = 0; consonant = 0; len = strlen(string); for(i=0; i<len; i++) { /* If the current character(string[i]) is a an alphabet */ switch(string[i]) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': vowel++; break; default: if((string[i]>='a' && string[i]<='z') || (string[i]>='A' && string[i]<='Z')) { consonant++; } } } printf("Total number of vowel = %d\n", vowel); printf("Total number of consonant = %d\n", consonant); return 0; }
Output
Enter any string: I love CodeforWin!
Total number of vowel = 7
Total number of consonant = 8
Total number of vowel = 7
Total number of consonant = 8
Happy coding ;)
You may also like
- String programming exercises and solutions.
- C program to convert uppercase string to lowercase string.
- C program to convert lowercase string to uppercase string
- C program to find length of a string.
- C program to find reverse of a given string.
- C program to check whether a string is palindrome or not.
- C program to find total number of words in a string.
- C program to find reverse of a string.
- C program to find first occurrence of a character in string.