7.6. Looping and countingΒΆ

The following program counts the number of times the letter β€œr” appears in a string:

Activity: CodeLens 7.6.1 (strCount)

This program demonstrates another pattern of computation called a counter. The variable count is initialized to 0 and then incremented each time an β€œr” is found. When the loop exits, count contains the result: the total number of r’s.

There are four errors in the following code. Fix the function so it takes a string and a character as arguments and returns the count of the character within the string.

In line 2, lettercount should be initialized to 0. In line 4, there should be a ==, not a =. In line 5, the variable name should be changed to lettercount. In line 6, the return statement should be indented.

Show Comments
You have attempted 1 of 5 activities on this page