Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

question is I have to find frequency of a character in a string . this code of mine isn't working.

0 votes
asked Nov 9, 2018 by Toshi Mudgal (160 points)
# include <stdio.h>
# include <string.h>
void main()
{  char a[30],h;
    int n,i;
    printf("enter line");
    n=strlen(a);
    for(i=0;i<n;i++)
    scanf("%c",&a[i]);
    printf("enter character");
    scanf("%c",&h);
    function(&a,&h,n);
}
void function(char *a,char *h,int n)
{int i,count=0;
    for(i=0;i<30;i++)
    {
        if(a[i]==h)
        count++;
        
    }
    if(count==0)
    printf("element not found");
    else
    printf("element found %d times",count);
    
}

1 Answer

0 votes
answered Nov 12, 2018 by anonymous

# include <stdio.h>
# include <string.h>
void main()
{  char a[30],h;
    int n,i;
    printf("enter line");
    n=strlen(a);
    for(i=0;i<n;i++)
    scanf("%c",&a[i]);
    printf("enter character");
    scanf("%c",&h);
    function(&a,&h,n);
}
void function(char *a,char *h,int n)
{int i,count=0;
    for(i=0;i<30;i++)
    {
        if(a[i]==*h) //you used if(a[i]==h) it simply refers address rather than character inside it  you must use * symbol before h
        count++;
        
    }
    if(count==0)
    printf("element not found");
    else
    printf("element found %d times",count);
    
}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...