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
    • 589
    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.

    1. #include <stdio.h>
    2. int main(){
    3. char c[1000],char;
    4. int i,count=0;
    5. printf("Enter any string ");
    6. gets(c);
    7. printf("Enter any character whose frequency u want to find: ");
    8. scanf("%c",&char);
    9. for(i=0;c[i]!='\0';++i)
    10. {
    11. // comparing string with the character whose frequency we want to find.
    12. if(char==c[i])
    13. // increment the count variable if the same character repeated.
    14. ++count;
    15. }
    16. printf("Frequency of %c = %d", char, count); // printing the frequency of the character.
    17. return 0;
    18. }

    Output:

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

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: