Monday, April 28, 2025

C Program - Find Frequency of a Character in a String

This C program prompts the user to enter a string and a character, then calculates and displays the frequency of the character in the string. 

#include <stdio.h>

int main()
{

    char some_string[1000], ch;
    int counter = 0;

    printf("Enter Some String: \n");
    gets(some_string);

    printf("Enter a Character: \n");
    scanf("%c", &ch);

    for (int x = 0; some_string[x] != '\0'; x++)
    {
        if (ch == some_string[x])
        {
            counter++;
            //printf("Counter atm: %d ", counter);
            //printf("Detected :%c \n", ch);
        }
    }

    printf("Frequency of %c character: %d \n", ch, counter);

    return 0;
}

Result: 

Enter Some String:
this is some test
Enter a Character:
t
Frequency of t character: 3

Process returned 0 (0x0)   execution time : 5.396 s
Press any key to continue.

Here's a detailed explanation of each block of code in the program: 

#include <stdio.h>

This line includes the standard input-output library header file. 

int main()
{

This line declares the main function that runs when the program is executed. The int keyword specifies that the function returns an integer value (0 in this case). 

char some_string[1000], ch;
int counter = 0;

This block of code declares three variables:

  • some_string: a character array that can hold up to 1000 characters
  • ch: a single character variable to store the character to be searched for
  • counter: an integer variable that will be used to count the frequency of the character in the string 
printf("Enter Some String: \n");
gets(some_string);

These lines prompt the user to enter a string and then reads the input string into the some_string array using the gets() function. Note that gets() is generally considered unsafe to use because it can cause a buffer overflow if the user inputs more than the allocated space for some_string. It is recommended to use fgets() instead, which limits the amount of input read. 

printf("Enter a Character: \n");
scanf("%c", &ch);

These lines prompt the user to enter a character and then reads the input character into the ch variable using the scanf() function. 

for (int x = 0; some_string[x] != '\0'; x++)
{
    if (ch == some_string[x])
    {
        counter++;
    }
}

This block of code is a for loop that iterates over the characters in the some_string array using an index variable x. The loop continues until it reaches the null character '\0', which marks the end of the string. Inside the loop, the program checks if the current character at index x is equal to the character ch. If it is, the counter variable is incremented. 

printf("Frequency of %c character: %d \n", ch, counter);

This line uses printf() to display the frequency of the character in the string. The %c format specifier is used to print the character ch, and the %d format specifier is used to print the integer counter

return 0;

This line returns an integer value of 0 to the operating system to indicate that the program executed successfully.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...