In this tutorial you will learn about the C Program to Count Vowels, and Consonants in a String and its application with practical example.
C Program to Count Vowels, and Consonants in a String
In this tutorial, we will learn to create a C program that will Count the Vowels and Consonants in a String in C programming
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in c programming.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the variables for the program. 2. Taking the input number from the user. 3. Adding the given sting to program. 4. Passing those variables to for loop. 5. Using those numbers to be saved in variables and filter them with for loop. 6. Printing the result words. |
Count the Vowels and Consonants in a String
As we all know, the String is a collection of all characters. In strings, only one variable is declared which can store multiple values. First, will take the line of string from the user. Then will filter the number of vowels and the consonants from that strings. At last, we will print the result numbers.
With the help of this program, we can Count the Vowels and Consonants in a String.
Program Code:-
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 |
#include <stdio.h> #include <string.h> int main() { /* declaring the required variables for the program */ char s[1000]; int i,vowels=0,consonants=0; /* Taking the input string from the user for the program */ printf("Enter the string : "); gets(s); /* Filtering the vowels and the consonants from the string */ 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++; } } /* Printing the number of vowels in that string */ printf("vowels = %d\n",vowels); /* Printing the number of consonants in that string */ printf("consonants = %d\n",consonants); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- s[1000] = it will hold the string value.
- Vowels = it will hold the integer value for vowels.
- Consonant = it will hold the integer value for consonants.
- i = it will hold the integer value.
Input strings from the user.
Filtering the vowels and the consonants from the string.
Printing output numbers of the vowels and the consonants.