In this article, we will show you, How to 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, it will search and count the total number of times this character has occurred inside a string.
/* C Program to 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; }
OUTPUT
ANALYSIS
First we used For Loop to iterate each and every 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 While Loop we used If Statement to check whether the str[0] is equal to user specified character or not
if(str[i] == ch) => if(t == t)
Above condition is True. So, 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)
Above condition is false. So, i value will be incremented.
Third Iteration: for(i = 2; 2 <= 16; 2++)
if(str[i] == ch) => if(t == t)
Above condition is True.
Count++
It means Count = 2
Do the same for the remaining iterations.
C Program to find All Occurrence of a Character in a String Example 2
This program is same as above. Here, we just replaced the For Lop with While Loop. I suggest you to refer While Loop to understand the Loop iterations.
/* C Program to 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; }
OUTPUT
C Program to find All Occurrence of a Character in a String Example 3
This program is same as first example but this time we used the Functions concept to separate the logic.
/* C Program to 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; }
OUTPUT
Thank You for Visiting Our Blog