简体   繁体   中英

How to find the most frequent vowel and consonants in the text in C

For example the user entered this text: How arrree yooou

output ==> the most frequent vowel is: o
the most frequent consonants is: r

Not: Here 'e' also a duplicate, but the most frequent one is 'o', I am looking for the most frequent

/* Initializes frequency of all characters to 0 */
for(i=0; i<MAX_CHARS; i++)
{
    freq[i] = 0;
}
/* Finds frequency of each characters */
i=0;
while(str[i] != '\0')
{
    ascii = ((int)str[i]=='a' || str[i]=='e' || str[i]=='i' ||
       str[i]=='o' || str[i]=='u' || str[i]=='A' ||
       str[i]=='E' || str[i]=='I' || str[i]=='O' ||
       str[i]=='U');

    vowels++;

    freq[ascii] += 1;

    i++;
}

/* Finds maximum frequency */
max = 0;
for(i=0; i<MAX_CHARS; i++)
{
    if(freq[i] > freq[max])
        max = i;
}


printf("Maximum occurring character is '%c' = %d times.", max, freq[max]);

return 0;

}

Since I was explicitly asked in the comments, you could do something like:

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

struct max {
        int count;
        int value;
};
/* CAUTION: this incorrectly reports that '\0' is a vowel.
   exercise for the reader. */
int isvowel(int c) 
{ 
        return strchr("aeiou", tolower(c)) != NULL;
}
int isconsonant(int c) 
{ 
        return isalpha(c) && ! isvowel(c);
}

int main(void)
{
        int c;
        struct max consonant = {0}, vowel = {0};
        int count[256] = {0};
        while( ( c = fgetc(stdin)) != EOF ) {
                assert( c < 256 );
                count[c] += 1;
                if( isvowel(c) && count[c] > vowel.count ) {
                        vowel.count = count[c];
                        vowel.value = c;
                } else if( isconsonant(c) && count[c] > consonant.count ) {
                        consonant.count = count[c];
                        consonant.value = c;
                }
        }
        printf("%c appears %d time%s, %c appears %d time%s\n",
                vowel.value, vowel.count, vowel.count > 1 ? "s" : "",
                consonant.value, consonant.count, consonant.count > 1 ? "s" : "" );
        return 0;
}

Here's a short solution I had fun assembling.

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

void get_most_frequent(int count[], const char const* set, char* letter, int *number)
{
    *letter = '\0';
    *number = 0;
    for(int i=0; set[i]; ++i)
    {
        *letter = (count[set[i]-'a'] > *number)? set[i]            : *letter;
        *number = (count[set[i]-'a'] > *number)? count[set[i]-'a'] : *number;
    }
}

int main(void) {
    const char str[] = "My sample text, that Contains many vowels And consanants of all shapes and sizes; along with UPPER and LOWER cases.";
    const char vowels[] = "aeiou";
    const char cnsts[] = "bcdfghjklmnpqrstvwxyz";
    int max;
    char letter;
    int count[26] = {};

    for(int i=0; str[i]; ++i)
    {
        count[tolower(str[i])-'a'] += !!isalpha(str[i]);
    }

    // Most frequent vowel
    get_most_frequent(count, vowels, &letter, &max);
    printf("Vowel %c appears %d times\n", letter, max);

    // Most frequent consonant
    get_most_frequent(count, cnsts, &letter, &max);
    printf("Consonant %c appears %d times\n", letter, max);

    return 0;
}

Output

Success #stdin #stdout 0s 4532KB
Vowel a appears 13 times
Consonant s appears 11 times

Since you've already figured out the most frequent vowel/ consonant, you might pass it to a switch case like this:

switch(c)
    {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
        case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U':
                    printf("\nThe most frequent vowel is: %c", c);
                    break;
        default:
                    printf("\nThe most frequent consonant is: %c", c);
    }

In case if you just want to know whether the most repeated character is a vowel/ consonant, the switch case could do the job. I haven't cheked your entire code, but if here:

printf("Maximum occurring character is '%c' = %d times.", max, freq[max]); 

max contains the most frequent character in your string, you could simply pass max to the switch case and print the output from the switch case itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM