Count Number of Occurrences of a Character
Count Number of Occurrences of a Character
Problem Statement:
The program must accept a string S and a character CH as the input. The program must print the number of occurrences of CH in the string S as the output.
Boundary Condition(S):
1 <= Length of S <=1000
Input Format:
The first line contains the string S.
The second line contains the character CH.
Output Format:
The first line contains the number of occurrences of CH in the string S.
Example Input/Output 1:
Input:
pineapple
p
Output:
3
Explanation:
The character p has occurred three times in the string "pineapple". So 3 is printed as the output.
Example Input/Output 2:
Input:
Encyclopedia
e
Output:
1
Explanation:
The character e has occurred only once in the string "Encyclopedia". So 1 is printed as the output.
Example Input/Output 3:
Input:
KEYBOARD
y
Output:
0
Note: Max Execution Time Limit: 4000 millisecs
Solution:
(Note: Just incase the code shows any error kindly comment us with your feedback to help us improvise from our mistake.)
Programming Language: C Language
#include <stdio.h> int main() { char str[101],ch; scanf("%s\n%c",str,&ch); int i=0,j=0; while(str[i]!='\0'){ if(str[i]==ch){ j++; } i++; } printf("%d",j); }
Comments