ProgrammingHard

A loop calls strlen(s) on a fixed string of length 10^4 once per character of the same string. How many characters are read in total?

  1. A. About 10^4
  2. B. About 5 x 10^7
  3. C. About 10^8
  4. D. About 2 x 10^4

Correct answer: B. About 5 x 10^7

The i-th call reads about i characters, so the sum is about n^2 / 2 = 5 x 10^7.

More C Programming questions

Exams that ask this topic