154. Count Occurrences of a Character

EasyStringString

Given a string s and a character c, count and return the number of times character c appears in string s.

The comparison is case-sensitive.

Input: First argument: string s. Second argument: character c (a single character string).

Output: An integer representing the count of occurrences of c in s.

Examples

Example 1
Input: hello,l
Output: 2
Explanation: The character 'l' appears at indices 2 and 3 in 'hello', so count is 2.
Example 2
Input: mississippi,s
Output: 4
Explanation: 's' appears at positions 2, 3, 5, 6 in 'mississippi' → count = 4.
Example 3
Input: abcdefg,z
Output: 0
Explanation: 'z' does not appear in 'abcdefg', so count is 0.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →