How to Count Vowels and Consonants in a String in C Language?
In this article, we will see how to count the number of vowels and consonants or words in a string. We want to know how many words are there. And also, we want to count the number of vowels and consonants in a string. First, let us show you vowels and consonants. Here we have taken a string:

So, this is a string “I am Rahul”. As we can clearly see there are 4 (a, I, u) vowels and 4 (m, R, h, l) consonants.

Here we have taken a string in the form of an array. We will take a vowel counter here as vCount and if any vowel is found we increment it. For that, we have to scan this string. So how to check whether it is a, e, i, o, u?

Then also it is uppercase and lowercase? Here we will write separate conditions. Different conditions for lower cases and upper cases will be written.

If we want to count consonants, for that we can include one more count as cCount.

Spaces will also be counted, so we should not take any special characters or spaces. We have to pick only the alphabet. So, make sure that it is within the range of the alphabet. Let’s see the code part and solve the above issues:
Counting Vowels and Consonants in a String Code in C language:
#include <stdio.h> #include <stdlib.h> int main () { char B[] = “I am Rahul”; int i, vCount = 0, cCount = 0; for (i = 0; B[i] != ‘\0’; i++) { if (B[i] == ‘a’ || B[i] == ‘e’ || B[i] == ‘i’ || B[i] == ‘o’ || B[i] == ‘u’ || B[i] == ‘A’ || B[i] == ‘E’ || B[i] == ‘I’ || B[i] == ‘O’ || B[i] == ‘U’) { vCount++; } else if ((B[i] >= 65 && B[i] <= 90) || (B[i] >= 97 && B[i] <= 122)) { cCount++; } } printf (“String is \”%s\”\n”, B); printf (“Vowels: %d\n”, vCount); printf (“Consonants: %d\n”, cCount); }
Output:

Counting words in a String in C language:
So, this is how we can count vowels and consonants. Now next let us see how to count the number of words in a string.

How many words are there? 3 words and 2 spaces are there. It means spaces will help us to identify words. So, simply count the spaces and plus 1. Then we get the number of words in a string. Just we have to check for the spaces. So, we count words and then total how many words are there? Only 3 words are there but Let us modify the above string as:

Still, there are 3 words but the number of spaces is 4 and if we add one to it then it will be 5. And 5 is not the answer. Here we have to deal with excess spaces. So, when you have a convenient set of spaces it is actually called white space. So, if there are any white spaces, we should check that also.
So let us see how to do that. Whenever you are having a space check that a previous one is also a space. If so then count that. Below is the code for finding the total number of words in a string:
Counting words in a String Code in C language:
#include <stdio.h> #include <stdlib.h> int main () { char B[] = “I am Rahul”; int i, word = 1; for (i = 0; B[i] != ‘\0’; i++) { if (B[i] == ‘ ‘ && B[i – 1] != ‘ ‘) { word++; } } printf (“String is \”%s\”\n”, B); printf (“Total Words: %d\n”, word); }
Output:
