Saturday, 3 May 2014

Write a C program to find frequency of a given character in a given string. e.g, str="assam" ch=s; frequency=2

#include<stdio.h>
#include<string.h>
main()
{
    char str[100],ch='a';
    int i,f=0;
    printf("\nEnter a string=");
    gets(str);
    //scanf("%s",str);
    printf("\nEnter a character=");
    scanf("%c",&ch);
    for(i=0;i<strlen(str);i++)
    {
        if(str[i]==ch)
            f++;
    }

    printf("\nFrequency of %c in %s is %d",ch,str,f);
}

No comments:

Post a Comment