Write a C program to find the number of vowels and consonants in a given string.
#include <string.h>
int main() {
char str[100];
int vowels = 0, cons = 0, i;
printf("Enter your string:\n");
fgets(str, 100, stdin);
str[strlen(str) - 1] = '\0';
for (i = 0; i < strlen(str); i++) {
if (isalpha(str[i]) == 0)
continue;
switch (str[i]) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vowels++;
break;
default:
cons++;
break;
}
}
printf("No of vowels: %d\n", vowels);
printf("No of consonants: %d\n", cons);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your string:
Apple And Orange
No of vowels: 6
No of consonants: 8
Enter your string:
Apple And Orange
No of vowels: 6
No of consonants: 8
No comments:
Post a Comment