C program to count the total number of occurrences of a character in a string using a while loop. | सी प्रोग्राम एक लूप का उपयोग करके स्ट्रिंग में एक चरित्र की घटनाओं की कुल संख्या को गिनने के लिए।












C program to count the total number of occurrences of a character in a string using a while loop.




C program to count the total number of occurrences of a character in a string using a while loop.



#include <stdio.h> #define MAX 100 int main() { char text[MAX]; char search; int i, count; printf("\n Enter any string: "); gets(text); printf("\n Enter any character to search: "); search = getchar(); count = 0; i=0; while(text[i] != '\0') { if(text[i] == search) { count++; } i++; } printf("\n Total occurrence of words in the given string '%c' = %d", search, count); return 0; }


Output:
Enter any string: I love c programming language Enter any character to search: o Total occurrence of words in the given string 'o' = 2



Exercise: 1
C program to count occurrences of a character in a string.