Count a Character in a String
DiscordData Scientist · Technical ScreenLast updated
Count case-sensitive occurrences of a character in a string, including empty input and repeated matches.
Count a Character in a String
Implement number_of_character(string, char).
stringis the input string.charis the single character whose occurrences you must count.
Return an integer giving the number of times char appears in string. Comparison is case-sensitive: uppercase and lowercase forms are different characters. Count every matching occurrence, including repeated adjacent occurrences.
Return zero if the input string contains no occurrence of char, including when the input string is empty. The returned count is a scalar, so its value is the complete output.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.