C Program to count vowels & consonants of a sentence

February 22, 2022, Learn eTutorial
1480

What are vowels, consonants, and special characters in a string?

The English alphabet consists of 26 letters where 5 of which are called vowels and the rest of the 21 letters are called consonants. "a, e, i, o, u" are the vowels in the English language, all other than this are consonants. Special characters include space, tab, newline, etc. This C program counts the number of vowels and consonants and it is related to strings and characters.

How to count the vowels and consonants in C?

Firstly, we have to check each letter in the given sentence and increment the respective count for a vowel or a consonant. For that, first set the counters to zero. Read a sentence from the user and take each character of the string using `for loop` until it reaches the end of the sentence, a null character. Then check each character of the sentence is whether it's a vowel or not by using the `if condition`. If it is a vowel, then increment its counter otherwise, the consonant counter is incremented.

After checking all characters in the sentence, we will display the number of vowels and consonants

ALGORITHM

STEP 1: Include the Header files to use the built-in functions in the C program.

STEP 2: Declare character arrays sentence[100]vow[ ]="aeiou"

STEP 3: Initialize the counter variables v_count c_count and set it to 0.

STEP 4: Accept the sentence from the user to the variable sentence using the `fgets()` function.

STEP 5: Using a `for loop`, take each character of the string and do step 6 until the last character

STEP 6: First, check if the character is not a Special character then do step 7 else go for the next iteration of the loop.

STEP 7: Using `strchr()` function checks if the character is present in vowels array vow[ ], both upper and lower cases are checked here converting the character by `tolower()` function. If it is a Vowel, then increment v_count count by 1 else increment the c_count count by 1.

STEP 8: Display the number of vowels in the v_count counter and the number of consonants in the c_count counter.

For counting the vowels and consonants, we are using the below concepts in C programming, please refer to those for a better understanding

C Source Code

```                                          ```#include <stdio.h>
#include <string.h>
#include<ctype.h>

void main() {

char sentence[100] , vow[] = "aeiou" ;
int i , v_count = 0 , c_count = 0 ;
char * st ;

printf ("Enter a sentence\n") ;
fgets (sentence, 100, stdin) ;
for (i = 0; sentence[i] != '\0'; i++) {
if (sentence[i] != '\t' && sentence[i] != ' ' && sentence[i] != '\n') {
st = strchr ( vow, tolower(sentence[i]) ) ;  /*checking for vowels in both lower and upper case */
if (st != NULL)
v_count = v_count + 1 ;  /* increment vowel counter */
else
c_count = c_count + 1 ;   /* increment consonant counter */
}
}
/* display output number of vowels and consonants */
printf("\nNo. of vowels is %d", v_count );
printf("\nNo. of consonants is %d ", c_count );
}```
```

OUTPUT

```Enter a sentence
Good Morning

No. of vowels is 4
No. of consonants is 7```

VIEW ALL
VIEW ALL