codescracker
c

C Program to Find Frequency of Character in String



« Previous Program Next Program »

In this tutorial, you will learn and get code about finding the frequency of a character in a given string. That is, how many times a particular character present in a given string. For example, if user enters the string as codescracker, and wants to check for the frequency of a character say c. Then it will be 3. Because c occurs 3 times in the given string codescracker

The question is, write a program in C to find frequency of any given character in a given string. Both string and character will be entered by user (at run-time). The answer to this question is:

// Find Frequency of Character in a String
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    int i, count=0;
    char str[100], ch;
    printf("Enter the String: ");
    gets(str);
    printf("Enter any character (present in string) to find its frequency: ");
    scanf("%c", &ch);
    for(i=0; str[i]!='\0'; i++)
    {
        if(ch==str[i])
            count++;
    }
    printf("\nFrequency of %c = %d", ch, count);
    printf("\n\n%c occurs %d times in %s", ch, count, str);
    getch();
    return 0;
}

As the above program is written under Code::Blocks IDE, therefore after successful build and run, here is its sample run:

frequency of character c language

Now enter any string say codescracker and press ENTER key. Then enter any character that presents inside the string codescracker say c and press ENTER again, to see the frequency of c in codescracker:

c program find frequency of character

Program Explained

  1. Get string input from user using gets() function
  2. Get character input from user using scanf() function
  3. Now We've created a for loop to check each and every character of the string, that is from the 0th character of the string to the null terminated character '\0' of the string
  4. Whenever the character matches with the character at any place of the string, then increment the count variable's value
  5. As count variable intially intialized to 0, therefore count holds the value as total times the character presents inside the string
  6. For example, let's suppse the string is codescracker (in str variable) and the character is c (in ch variable)
  7. Therefore, str[0] holds c, str[1] holds o, str[2] holds d, str[3] holds e, ....., str[9] holds k, str[10] holds e, and str[11] holds r
  8. The character c presents at the index number 0, 5, and 8. Total of 3 times.
  9. Therefore the count variable gets incremented 3 times. As the condition ch==str[i] gets true three times
  10. Because ch holds c and str[0], str[5], and str[8] also holds c.
  11. Therefore, the frequency of c in the given string say codescracker will be 3

Find Frequency of each Character in a String

Now let's modify the above program and in place of finding the frequency of one particular character, let's find frequency of each and every character present in the given string:

// Find Frequency of Each Character in a String
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char str[100], ch;
    int i, j, k, len, count=0;
    printf("Enter the string: ");
    gets(str);
    len = strlen(str);
    printf("\n");
    for(i=0; i<len; i++)
    {
        ch = str[i];
        for(j=0; j<len; j++)
        {
            if(ch==str[j])
            {
                count++;
                for(k=j; k<(len-1); k++)
                {
                    str[k] = str[k+1];
                }
                len--;
                str[len] = '\0';
                j--;
            }
        }
        printf("%c = %d times\n", ch, count);
        count=0;
        i--;
    }
	getch();
	return 0;
}

Here is the sample run:

c program frequency of each character string

Now supply any string say we love codescracker and press ENTER to see the frequency of all the character one by one as shown in the screenshot given below:

find frequency of each character in c

Program Explained

  1. Here we have used one outer loop that runs until the null terminated character ('\0') occurs in the string
  2. And in inner loop we have used the same loop with another variable
  3. Before inner loop We've intialized the initial character of the string to the ch variable
  4. And then comes inside the inner loop
  5. In this loop we have performed checking operation that where-where ch's value is present inside the string, if present then we have incremented the count variable and then shifted one index back all the character of the string, that is str[1] is shifted to str[0], str[2] shifted to str[1], and so on.
  6. As the character is counted up, therefore character at that particular place has not any further use, therefore we have shifted and deleted that character from the string using the second inner for loop using another variable
  7. After breaking out of this loop, decrement the string length say len here, and intialized null terminated character ('\0') to the last index of the string
  8. And then decrement the second inner for loop variable and continue to run until the condition of first inner for loop evaluates to be false
  9. After this, print the output and initlized count as 0 and decrement the value of outer loop
  10. In this way frequency of all the character is calculated and printed one by one

Print Frequency with Remaining Character

Here is another program that list out all the character's frequency along with remaining character in string:

// Find Frequency of Each Character
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char str[100], ch;
    int i, j, k, len, count=0;
    printf("Enter the string: ");
    gets(str);
    len = strlen(str);
    printf("\n");
    printf("Character\tCount\t\tString Left\n\n");
    for(i=0; i<len; i++)
    {
        ch = str[i];
        for(j=0; j<len; j++)
        {
            if(ch==str[j])
            {
                count++;
                for(k=j; k<(len-1); k++)
                    str[k] = str[k+1];
                len--;
                str[len] = '\0';
                j--;
            }
        }
        printf("%c occurs \t%d times\t\t%s\n", ch, count, str);
        count=0;
        i--;
    }
    getch();
    return 0;
}

Here is its sample run:

frequency of character

Now supply any string say codescracker and press enter to check it out:

find frequency of character in string c program

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker