Saturday 11 November 2017

C Program to Count the Number of Occurrence of each Character Ignoring the Case of Alphabets & Display them


Code:

#include
#include
#include

struct detail
{
    char c;
    int freq;
};

int main()
{
    struct detail s[26];
    char string[100], c;
    int i = 0, index;

    for (i = 0; i < 26; i++)
    {
       s[i].c = i + 'a';
       s[i].freq = 0;
    }
    printf("Enter string: ");
    i = 0;
    do
    {
        fflush(stdin);
        c = getchar();
        string[i++] = c;
        if (c == '\n')
        {
            break;
        }
        c = tolower(c);
        index = c - 'a';
        s[index].freq++;
    } while (1);
    string[i - 1] = '\0';
    printf("The string entered is: %s\n", string);

    printf("*************************\nCharacter\tFrequency\n*************************\n");
    for (i = 0; i < 26; i++)
    {
        if (s[i].freq)
        {
            printf("     %c\t\t   %d\n", s[i].c, s[i].freq);
        }
    }

    return 0;
}


Output:

Enter string: A quIck brOwn fox JumpEd over a lazy dOg
The string entered is: A quIck brOwn fox JumpEd over a lazy dOg
*************************
Character Frequency
*************************
     a                3
     b                1
     c                1
     d                2
     e                2
     f                1
     g                1
     i                1
     j                1
     k                1
     l                1
     m                       1
     n                1
     o                4
     p                1
     q                1
     r                2  
     u                2
     v                1
     w                       1
     x                1
     y                1
     z                1



More C Programs:














100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...