C Program to Find Frequency and Position of Characters in a String

This is a C program to check whether a given character is present in a string, find frequency & position of occurrence.

Problem Description

This C Program checks whether a given character is present in a string, find frequency & position of occurrence.

Problem Solution

This C Program checks whether a given character is present in a string, it also displays it occurrence and frequency as shown in the program below.

Program/Source Code

Here is a source code of the C program to check if a given character is present in a string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/* 
 * C Program to Check whether a given Character is present in a
 * String, Find Frequency & Position of Occurrence 
 */
#include <stdio.h>
#include <string.h>
 
int main()
{
    char a, word[50];
    int i, freq = 0, flag = 0;
 
    printf("Enter character: ");
    scanf("%c", &a);
    printf("Now enter the word: ");
    scanf("%s", word);
    printf("Positions of '%c' in %s are: ", a, word);
    for (i = 0; i < strlen(word); i++)
    {
        if (word[i] == a)
        {
            flag = 1;
            printf("%d  ", i + 1);
            freq++;
        }
    }
    if (flag)
    {
        printf("\nCharacter '%c' occured for %d times.\n", a, freq);
    }
    else
    {
        printf("None\n");
    }
 
    return 0;
}
Program Explanation

In this C program, we are reading the character and the word to check the position of occurrence using ‘a’ and ‘word’ variables respectively. In for loop initialize the value of ‘i’ variable as 0. Check the length of the string is greater than the value of ‘i’ variable.

advertisement
advertisement

Nested if else condition statement is used to check whether a given character is present in a string using the word[] variable is equal to the value of ‘a’ variable. If the condition is true then execute the statement. Initialize the value of ‘flag’ variable as 1 and increment the value of ‘freq’ variable. Print the characters, frequency & position of occurrence in a string.

Runtime Test Cases
 
$ cc charfrequency.c
$ ./a.out
Enter character: r
Now enter the word: programming
Positions of 'r' in programming are: 2  5  
Character 'r' occured for 2 times.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.