Write a C Program to Count All Occurrence of a Character in a String with example.
C Program to Count All Occurrence of a Character in a String Example 1
This program allows the user to enter a string (or character array), and a character value. Next, the C program will search and count the total number of times this character has occurred inside a string.
/* Count All Occurrence of a Character in a String */
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], ch;
int i, Count;
Count = 0;
printf("\n Please Enter any String : ");
gets(str);
printf("\n Please Enter the Character that you want to Search for : ");
scanf("%c", &ch);
for(i = 0; i <= strlen(str); i++)
{
if(str[i] == ch)
{
Count++;
}
}
printf("\n The Total Number of times '%c' has Occured = %d ", ch, Count);
return 0;
}
In this Program to Count All Occurrence of a Character in a String, First, we used For Loop to iterate each character in a String.
for(i = 0; i <= strlen(str); i++) { if(str[i] == ch) { Count++; } }
str[] = tutorial gateway
ch = t
Count = 0
For Loop First Iteration: for(i = 0; i <= strlen(str); i++)
The condition is True because 0 <= 16.
Within the C Programming While Loop, we used If Statement to check whether the str[0] is equal to a user-specified character or not
if(str[i] == ch) => if(t == t)
The above condition is True. So, the compiler will execute the statement inside the If block.
Count++
it means Count = 1
Second Iteration: for(i = 1; 1 <= 16; 1++) – The condition is True because 1 <= 16.
if(str[i] == ch) => if(t == l) – condition is false. So, i value will increment.
Third Iteration: for(i = 2; 2 <= 16; 2++)
if(str[i] == ch) => if(t == t) – condition is True.
Count++
It means Count = 2
Do the same for the remaining iterations.
C Program to count Occurrence of a Character in a String Example 2
This program to Count All Occurrence of a Character is the same as above. Here, we just replaced the For Loop with the While Loop.
/* Count All Occurrence of a Character in a String */
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], ch;
int i, Count;
i = Count = 0;
printf("\n Please Enter any String : ");
gets(str);
printf("\n Please Enter the Character that you want to Search for : ");
scanf("%c", &ch);
while(str[i] != '\0')
{
if(str[i] == ch)
{
Count++;
}
i++;
}
printf("\n The Total Number of times '%c' has Occured = %d ", ch, Count);
return 0;
}
Please Enter any String : hello world
Please Enter the Character that you want to Search for : l
The Total Number of times 'l' has Occured = 3
Program to find All Occurrence of a Character in a String Example 3
This program is the same as the first example, but this time we used the Functions concept to separate the logic.
/* Count All Occurrence of a Character in a String */
#include <stdio.h>
#include <string.h>
int Count_AllCharcater(char str[], char ch);
int main()
{
char str[100], ch;
int Count;
Count = 0;
printf("\n Please Enter any String : ");
gets(str);
printf("\n Please Enter the Character that you want to Search for : ");
scanf("%c", &ch);
Count = Count_AllCharcater(str, ch);
printf("\n The Total Number of times '%c' has Occured = %d ", ch, Count);
return 0;
}
int Count_AllCharcater(char str[], char ch)
{
int i, Count;
Count = 0;
for(i = 0; i <= strlen(str); i++)
{
if(str[i] == ch)
{
Count++;
}
}
return Count;
}
Please Enter any String : learn c programming with many examples
Please Enter the Character that you want to Search for : a
The Total Number of times 'a' has Occured = 4