ProgrammingHardTCS NQT · Accenture

The procedure below counts the positions i in "mississippi" where the two letters s[i]s[i+1] are both "s" (overlaps allowed, though "ss" cannot overlap itself). What is printed?

The procedure below counts the positions i in "mississippi" where the two letters s[i]s[i+1] are both "s" (overlaps allowed, though "ss" cannot overlap itself). What is printed?
count = 0
for i = 0 to n - 2
    if s[i] == 's' and s[i+1] == 's' then count = count + 1
end for
Print count
  1. A. 2
  2. B. 0
  3. C. 1
  4. D. 4

Correct answer: A. 2

The double s appears at positions 2-3 and 5-6, so the count is 2. The double p gives 1, a double i does not occur at all, and the number of letters i is 4.

More Pseudocode and Code Tracing questions

Exams that ask this topic