C program to count the occurrences of a character in a string

Ans: 

In this tutorial, you will learn writing program to 

count occurrences of a character in a string.

The Complete logic behind to count character in a string is given below :

  • In the first place, our program should take a string.
  • And also a character which will be given by the user for which we want to count the occurrence in string.
  • Then our program will count the total number of occurrences of that particular character in a string.
  • For example, suppose some user has given a string ” quescol is a best educational website” and want to count the occurrence of the character ‘a’ in the given above string.
  • Then our program will give output 3. Because ‘a’ is repeated 3 times in that string.

How our program will behave?

Our program will take a string as an input. A string could be with space or without space.
And also user have to give a character as an input for which they want to count the occurrence.
And in output, our program will give the total count of that particular character in the string.

For example:

Input: “Quescol provides previous year questions and answers” as string

and ‘a’ as a character

Output: 3

C Program to count the occurrence of given character in String

#include <stdio.h>
#include <conio.h>
#include <string.h>
int countOccurrence(char *str, char ch)
{
	int i, count = 0;
	int len = strlen(str);
	for (i = 0; i < len; i++)
	{
		if (str[i] == ch)
		{
			count++;
		}
	}
	return count;
}

void main()
{
	int count;
	char str[100], ch;
	printf("Enter a string: ");
	fgets(str, 100, stdin);
	printf("Enter a character you want to count: ");
	scanf("%c", &ch);
	count = countOccurrence(str, ch);
	printf("total occurence of '%c': %d", ch, count);
	getch();
}

Output:

Enter a string: Quescol Website
Enter a character you want to count: s
total occurence of 's': 2

Explanation of the above program

  • Above program is for how to count occurrence of a given character in a string in C.
  • In main() method, we have taken three char variable where str is an array of character to store string and a variable ch is to take a one character.
  • All the logic of counting the occurrence of a character in a string is written in the method countOccurrence() method.
  • Method countOccurrence() takes address of char array and a character as a parameter.
  • In countOccurrence() method there are three int variable i, count and len. count variable is responsible to count the number of times a character in string. 
  • str variable of countOccurrence() method contains string and ch variable has character.
  • Using for loop str is iterated each time and check if character of string is matching with the character which we want to count. 
  • If it matches then increase count by one otherwise do nothing just increase for loop times.
  • And at last when for loop is iterated for the the length of string then loop condition will fail and method will return total count of character.

This was the all logic behind to count the number occurrence of given character in string using C.

I hope it has cleared your concept.

Leave a Comment