Answer to Question #285818 in C for vin

Question #285818

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.


1
Expert's answer
2022-01-09T10:43:42-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS