Learn
Exam

Count the Occurrences of a Specific Character in a String

Difficulty
STARTER

Problem

Given a string word, write a function solution that returns the number of occurrences of a specific character c within the given string.


Constraints

  • word consists only of lowercase English letters.
  • The length of word is between 1 and 100 inclusive.
  • c is a single lowercase English letter.

Test Case

Test Case Number word c return
1 "hello" "l" 2
2 "algorithm" "g" 1
3 "banana" "a" 3

Explanation for Test Case 1

  • When word = "hello" and c = "l", there are 2 occurrences of "l" in the string, so the return value is 2.

Explanation for Test Case 2

  • When word = "algorithm" and c = "g", there is 1 occurrence of "g" in the string, so the return value is 1.

Explanation for Test Case 3

  • When word = "banana" and c = "a", there are 3 occurrences of "a" in the string, so the return value is 3.