# Algorithm and Flowchart to Count no. of Vowels, Consonants and Special Character in a string

[45879 views]

### What are Vowels?

In English language some of the alphabets a, e, i, o and u are known as the vowels.

### What are Consonants?

The rest all the alphabets of English language which are not vowels are known as Consonants

So in this program we will be counting Vowels, Consonants, Space, any special character or digits.

### What should be the approach for this program?

So what we can think is we have to count characters so it should definitely run though a loop therefore we will run loop through each characters and check whether it vowel, consonants, digits or any other thing. For that we have to use If and else loop we will check if that character is vowel or digits.

## Algorithm for counting no. of vowels, consonants, digits and any special character in the string

Declare a array of character s and i, digits, consonants, vowels and special as int; Read the string s; // digits, consonants, vowels and special var is for counting for each character in s do{ Check if(s[i] is between a and z) Check if(s[i] is a,e,i,o,u) vowels==vowels+1 else consonants=consonants+1 Check if(s[i] is between 0 and 9) digits=digits+1 else special=special+1 } Print digits, consonants, vowels and special;

Here in this algorithm we declare an array of characters which will be used to store the string. We declare digits, consonants, vowels and special for counting them and i for iterating the for loop. We use a for loop to iterate each character in the string. For Eg. Let's take "aebc 567 \$%&" , this is our String. So in the for loop once a will go then e will go likewise the loop will run. When the a will go it will check if it is between a and z if Yes then it will check whether it is a, e, i, o, u. If yes, then vowels will become 1 and if it is not then consonants will become 1. If digits the it will go in the if statement condition of between 0 and 9 and for else any other character even the space will be counted as special character.

## Implementation of the Program in C

#include<stdio.h> #include<string.h> int main() { char s[40]; int i, consonants = 0, vowels, special, digits; printf(" Enter the string:"); gets(s); for (i = 0; i < strlen(s); i++) { if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')) { 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++; } } else if (s[i] >= '0' && s[i] <= '9') { digits++; } else { special++; } } printf(" The String "); puts(s); printf(" Vowels:%d\n Consonants:%d\n Digits:%d\n Special Charcters:%d ", vowels, consonants, digits, special); }

Note:Here, we have used gets and puts to enter the string and not scanf("%s") because gets store even the space while the scanf will store the Characters till the space character and not after that.