Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Finding frequency of a character in a string

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 528
    Comment on it

    This program is all about finding how many times a character comes in a string. In the program there is a count variable which will be incremented if the same character appears again and then that count variable will be printed.

      #include <stdio.h>
        int main(){
           char c[1000],char;
           int i,count=0;
           printf("Enter any string ");
           gets(c);
           printf("Enter any character whose frequency u want to find: ");
           scanf("%c",&char);
           for(i=0;c[i]!='\0';++i)
           {                             
                // comparing string with the character whose frequency we want to find.
                 if(char==c[i])           
               // increment the count variable if the same character repeated.
                   ++count;
           }
           printf("Frequency of %c = %d", char, count);   // printing the frequency of the character.
           return 0;
        }
    

    Output:

    Enter a string: Welcome to Findnerd.
    Enter a character to find frequency: e
    Frequency of e = 3
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: