Count Character Occurrences
EasyPractice library · +10 XPstringsloopscounting
Given a string $S$ and a character $C$, count how many times the character $C$ appears in the string $S$.
The input will consist of two lines. The first line will contain the string $S$. The second line will contain the character $C$.
Print a single integer, the total count of character $C$ in string $S$.
Input
Line 1: String $S$ (contains only lowercase English letters). Line 2: Character $C$ (a single lowercase English letter).
Output
A single integer representing the count of character $C$ in $S$.
Constraints
1 <= |S| <= 1000
Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests
Example 1
Input
programming r
Output
2
The character 'r' appears 2 times in the string 'programming'.
Example 2
Input
hello world x
Output
0
The character 'x' does not appear in the string 'hello world', so the count is 0.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
programming r
Example 2 input
hello world x