Given an input string “Ayodhya”, your C program should count the number of vowels and consonants
and print the frequency of occurrence of each vowel and consonant. Delete the vowels in the same
input string and display the updated string.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
int main() {
char str[100];
char vowels[] = {'a', 'e', 'i', 'o', 'u'};
int N_VOWELS = sizeof(vowels) / sizeof(vowels[0]);
char consonants[] = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k',
'l', 'm', 'n', 'p', 'q', 'r', 's', 't',
'v', 'w','x', 'y', 'z'};
int N_CONS = sizeof(consonants)/sizeof(consonants[0]);
int count_vowels = 0, count_consonants = 0;
int i, j, k, n;
fgets(str, 100, stdin);
n = strlen(str);
if (str[n-1] == '\n') {
str[n-1] = '\0';
n--;
}
for (i=0, k=0; i<n; i++) {
char ch = tolower(str[i]);
bool is_vowel = false;
for (j=0; j<N_VOWELS; j++) {
if (ch == vowels[j]) {
count_vowels++;
is_vowel = true;
break;
}
}
for (j=0; j<N_CONS; j++) {
if (ch == consonants[j]) {
count_consonants++;
break;
}
}
if (!is_vowel) {
str[k] = str[i];
k++;
}
}
str[k] = '\0';
printf("There were %d vowels and %d consonants in the string\n",
count_vowels, count_consonants);
printf("New string is %s\n", str);
return 0;
}
Comments
Leave a comment