Skip to main content

Count the Characters

Problem Description​

Write a function that accepts a string and a character, and returns the number of times the specific character appears in the string, taking case into account.

Tip: If you can solve this without using a for-in loop, you can consider it a tricky challenge.

Input​

  • A string.
  • A single character.

Output​

  • An integer representing the number of times the specified character appears in the input string.

Constraints​

  • The function should take case into account (e.g., 'a' is different from 'A').

Example​

Input:
"The rain in Spain"
Character: "a"

Output:
2

Explanation:
The character 'a' appears 2 times in the string.

Input:
"Mississippi"
Character: "i"

Output:
4

Explanation:
The character 'i' appears 4 times in the string.

Input:
"Hacking with Swift"
Character: "i"

Output:
3

Explanation:
The character 'i' appears 3 times in the string.