C++ Program to Count Occurrence of Character
Write C++ Program to Count Occurrence of Character
// CPP Program to Count Occurrence of Character #include <iostream> using namespace std; int main() { int Count = 0; char S[] = "CodeCrucks", ch; int i = 0; cout << "String :--> " << S; cout << "\nEnter Character :--> "; cin >> ch; while(S[i] != '\0') { if (S[i] == ch) Count++; i++; } cout << "\nOccurrence of character :--> " << 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