In this tutorial you will learn about the C Program to Count the Number of Vowels, Consonants and so on and its application with practical example.
C Program to Count the Number of Vowels, Consonants and so on
In this tutorial, we will learn to create a C program that will Count the Number of Vowels, Consonants, and so on in C programming
Prerequisites
Before starting with this tutorial we assume that you are 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.
Count the Number of Vowels, Consonants, and so on
As we all know the String is a collection of character data types. In strings, only one variable is declared which can store multiple values. First will take the line of string from the user. Then will take the number of elements in the string, Now we will filter the string using the c programming.
With the help of this program, we can Count the Number of Vowels, Consonants, and so on.
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 forloop. 5. Using those numbers to be saved in variables and filter them with for loop. 6. Printing the result words. |
Program to Count the Number of Vowels, Consonants, and so on:-
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#include <stdio.h> int main() { //Declaring the required variables char line[150]; int vowels, consonant, digit, space; //Assigning blank value to all vowels = consonant = digit = space = 0; //Taking Input string From the user printf("Enter a line of string: "); fgets(line, sizeof(line), stdin); //Getting size of string from the user for (int i = 0; line[i] != '\0'; ++i) { ////filtering vowels from the string if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' || line[i] == 'o' || line[i] == 'u' || line[i] == 'A' || line[i] == 'E' || line[i] == 'I' || line[i] == 'O' || line[i] == 'U') { ++vowels; } //filtering consonants from the string else if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z')) { ++consonant; } //filtering numbers from the string else if (line[i] >= '0' && line[i] <= '9') { ++digit; } ////filtering white spaces from the string else if (line[i] == ' ') { ++space; } } //Printing number of vowels printf("Vowels: %d", vowels); //Printing number of consonants printf("\nConsonants: %d", consonant); //Printing number of Numbers printf("\nNumbers: %d", digit); //Printing number of White spaces printf("\nWhite spaces: %d", space); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- line[150] = it will hold the string value.
- vowels = it will hold the integer value for vowels.
- consonant = it will hold the integer value for consonants.
- digit= it will hold the integer value for numbers.
- space= it will hold the integer value for spaces.
Input strings from the user.
Assigning blank value to all to prevent garbage value.
Calculating the size of the string.
Filtering vowels from the string.
consonants filtering from the string.
filtering numbers from the string.
filtering white spaces from the string.
Printing output.