C Program to print the number of distict characters , the character which appears maximum times and the frequency of each character

 


// Program by Akash Tripathi (@proakash256) 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    printf("Enter a String : ");
    char *s;
    s = malloc(1024 * sizeof(char));
    gets(s);
    s = realloc(sstrlen(s) + 1);
    int c = 0 , max = 0 , index = 0;
    for(int i = 0i < strlen(s); i = i + 1)
    {
        c = 0;
        for(int j = 0j < strlen(s); j = j + 1)
        {
            if(s[j] == s[i])
                c = c + 1;
        }
        if(c > max)
        {
            max = c;
            index = i;
        }
    }
    int d = 1;
    for(int i = 1i < strlen(s); i = i + 1)
    {
        int j;
        for(j = 0j < ij = j + 1)
        {
            if(s[j] == s[i])
            {
                break;
            }
        }
        if(i == j)
            d = d + 1;
    }
    printf("\nThe number of distinct characters is %d" , d);
    printf("\n\nThe Character %c appears maximum  %d times\n\n" , s[index] , max);
    printf("Frequency of each element :\n");
    char fre[26];
    for(int i = 0i < 26i = i + 1)
    {
        fre[i] = 0;
    }
    for(int i = 0i < strlen(s); i = i + 1)
    {
        if(s[i] >= 'a' && s[i] <= 'z')
        {
            fre[s[i] - 97] += 1;
        }
    }
    for(int i = 0i < 26i = i + 1)
    {
        if(fre[i] != 0)
            printf("%c --> %d\n" , (i + 97) , fre[i]);
    }
    return 0;
}

Comments