← Back to Recursion
Question 604
Character Tally By Recursion
Character Tally By Recursion
Complete count_char(s: str, ch: str) -> int so it returns how many times the single character ch appears in the text s, by recursion. The base case is written: the empty string holds 0 matches. Add the recursive case: when the first character equals ch, return 1 plus the matches in the rest of the text, the slice s[1:]; otherwise return just the matches in the rest. Do NOT write a main block or any top-level test code; the tests construct the calls. Input: two lines, the text (it may be empty) and then the character, which the tests read and pass to your function before printing the value it returns.
Expected Output:
3= newline. The output ends with a newline — print one after the last line.
Hints3 available
Sign in to use hintsTopics:
RecursionSlicing
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.