Introduction:

This C program is designed to find the frequency of a specific character within a given string. The program prompts the user for both a string and a character, then counts how many times that character appears in the string. This functionality is useful in various applications such as text analysis, data validation, and character counting tasks.

#include <stdio.h>
#include <math.h>
void Solve()
{
    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);
}
int main()
{
    Solve();
    return 0;
}

Include Header Files:

  • #include <stdio.h>: This header file is included for standard input/output functions like printf and fgets.

Declare Variables:

  • char str[100];: An array to hold the input string. The size is set to 100, allowing up to 99 characters plus the null terminator.
  • char ch;: A variable to store the character whose frequency we want to count.
  • int count = 0;: A counter initialized to zero to track the frequency of the character.

Input the String:

  • printf("Enter a string: ");: Prompts the user to enter a string.
  • fgets(str, sizeof(str), stdin);: Reads a line of text from standard input, allowing for spaces, and stores it in str.
  • str[strcspn(str, "\n")] = 0;: Removes the newline character that fgets adds to the end of the string.

Input the Character:

  • printf("Enter a character to find its frequency: ");: Prompts the user to enter a character.
  • scanf("%c", &ch);: Reads a single character from input and stores it in ch.

Calculate the Frequency:

  • for (int i = 0; str[i] != '\0'; i++): This loop iterates through each character in the string until it reaches the null terminator ('\0').
  • if (str[i] == ch): Checks if the current character matches the specified character.
    • If there’s a match, count++ increments the frequency counter.

Output the Result:

  • printf("Frequency of '%c' in the string: %d\n", ch, count);: Displays the frequency of the specified character in the input string.

Return Statement:

  • return 0;: Indicates that the program executed successfully.

Input/Output Block:

Input:

  • The user is prompted to enter a string.
  • The user is then asked to specify the character for which the frequency is to be calculated.

Example Input:

Enter the String: Hello, World!
Enter any character (present in string) to find its frequency: o

Output:

  • The program outputs the frequency of the specified character in the string.

Example Output:

arduinoCopy codeFrequency of 'o': 2

Conclusion:

The character frequency program effectively counts the occurrences of a specified character within a string, demonstrating fundamental concepts of string manipulation and conditional logic in C. By providing a user-friendly interface for input, the program showcases its utility in various contexts, such as text processing and data analysis. This functionality can be expanded for more complex text analysis tasks, making it a valuable tool for developers and analysts alike.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights