C Program to Count Occurrence of Character
Write C Program to Count Occurrence of Character
// C Program to Count Occurrence of Character #include <stdio.h> int main() { int count = 0; char S[] = "CodeCrucks", ch; int i = 0; printf("String :--> %s", S); printf("\nEnter Character :--> "); scanf("%c", &ch); while(S[i] != '\0') { if (S[i] == ch) count++; i++; } printf("\nOccurrence of character :--> %d", count); return 0; }
Output:
Run 1:
String :--> CodeCrucks
Enter Character :--> C
Occurrence of character :--> 2
Run 2:
String :--> CodeCrucks
Enter Character :--> Z
Occurrence of character :--> 0