Thursday 20 December 2012

Frequency of letters in a String-NAGARRO's placement question

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

int main()
{
    int i=0;
    char str[20];
    int cap[26]={0};
    int small[26]={0};
    printf("\nEnter your string: ");
    gets(str);
    while(str[i]!='\0')
    {
        if (str[i]>='A'&&str[i]<='Z')
            cap[str[i]-'A']++;
        else if(str[i]>='a'&&str[i]<='z')
            small[str[i]-'a']++;
        i++;
    }
    int max=0;
    int capf = 1;
    int pos=0;
    for(i=0;i<26;i++)
    {
        if(max<cap[i])
        {
            max=cap[i];
            pos=i;
            capf=1;
        }
        if(max<small[i])
        {
            max=small[i];
            pos=i;
            capf=0;
        }
    }
    if (capf)
        printf("\nThe maximum occurance alphabet is %c with %d times",pos+'A',max);
    else
        printf("\nThe maximum occurance alphabet is %c with %d times",pos+'a',max);
    return 0;
}


-Gaurav Singhal

No comments:

Post a Comment