Write a C program to count the vowels in a string
Description: Take a word as input. Print how many vowels (a, e, i, o, u) it has.
Hint:
- Go through each character. Convert it to lower case, and check if it is one of the five vowels.
C program to count the vowels
#include <stdio.h>
#include <ctype.h>
int main(void) {
char s[50];
int count = 0;
printf("Enter a word: ");
scanf("%49s", s);
for (int i = 0; s[i] != '\0'; i++) {
switch (tolower((unsigned char)s[i])) {
case 'a': case 'e': case 'i': case 'o': case 'u':
count++;
}
}
printf("Vowels = %d\n", count);
return 0;
}
Output:
Output of the above program is
Enter a word: Education
Vowels = 5
How it works
- “Education” has the vowels E, u, a, i, o, so the answer is 5.
tolower()makes the check work for capital letters also.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |