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:
2Explanation:
The character 'a' appears 2 times in the string.
Input:
"Mississippi"
Character: "i"Output:
4Explanation:
The character 'i' appears 4 times in the string.
Input:
"Hacking with Swift"
Character: "i"Output:
3Explanation:
The character 'i' appears 3 times in the string.