C program to count the occurrences of a character in a given string – In this article, we will discuss the various means to count the occurrences of a character in a given string in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The means used in this piece are as follows:
- Using Standard Method
- Using Function
- Using Recursion
A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.
As you can see in the image uploaded above, firstly, you need to enter the string concerned.
The string here, in this case, is “welcome to cbeginners”.
Later, you have to assign a particular character to be searched in the string.
Character ‘e’ is to be searched in the said example.
Thus, character ‘e’ occurs around 4 times in the whole string.
Hence, the ways to do the same in C programming are as follows:
Using Standard Method
- Count=0,Read the entered string as gets(s).
2) Read the entered character c as getchar().
3) Compare the entered character with the elements of the string using for loop with the structure for(i=0;s[i];i++).
If the character match with s[i] then increase the count value by 1.
4) For each occurrence of the character, the count value will be increased. So after all iterations of for loop, we will get total occurrences of the character in the string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <stdio.h> #include <string.h> int main() { char s[1000],c; int i,count=0; printf("Enter the string : "); gets(s); printf("Enter character to be searched: "); c=getchar(); for(i=0;s[i];i++) { if(s[i]==c) { count++; } } printf("character '%c' occurs %d times \n ",c,count); return 0; } |
Output:
1 2 3 |
Enter the string: welcome to cbeginners Enter character to be searched: e character 'e' occurs 4 times |
Using Function
- Count=0.
The function check(char *s, char c),
a) Compare the given character with all elements of the string using for loop for(i=0;s[i];i++).
b) If it matches with the element of the string then increase the count value.
c) The function returns the count value to the main() function then main() prints the count value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <stdio.h> #include <string.h> int check(char *s,char c) { int i,count=0; for(i=0;s[i];i++) { if(s[i]==c) { count++; } } return count; } int main() { char s[1000],c; int count=0; printf("Enter the string : "); gets(s); printf("Enter character to be searched: "); c=getchar(); count=check(s,c); printf("character '%c' occurs %d times \n ",c,count); return 0; } |
Output:
1 2 3 |
Enter the string: hello good morning Enter character to be searched: o character 'o' occurs 4 times |
Using Recursion
- The function returns the default count value if there is no element exist in the string.
2) If the string contains elements and s[i] is not null then
The function compares the s[i] with the entered character. If both are equal then it increases the count value.
Increase the i value.
Call the function itself.
The function calls itself recursively until s[i] becomes null then the function returns the count value to the main() function.
3) The main() function prints the count value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#include <stdio.h> #include <string.h> int check(char *s,char c) { static int i=0,count=0; if(!s[i]) { return count; } else { if(s[i]==c) { count++; } i++; check(s,c); } } int main() { char s[1000],c; int count=0; printf("Enter the string : "); gets(s); printf("Enter character to be searched: "); c=getchar(); count=check(s,c); printf("character '%c' occurs %d times \n ",c,count); return 0; } |
1 2 3 4 |
Enter the string: A man who dares to waste one hour of time has not discovered the value of life Enter character to be searched: a character 'a' occurs 5 times |