ProgrammingHardProduct company OA

What is printed?

What is printed?
const char *s = "abcabcab";
const char *p = strstr(s, "cab");
printf("%td %s %td", p - s, p + 3, strstr(p + 1, "cab") - s);
  1. A. 2 abcab 5
  2. B. 2 cab 5
  3. C. 5 cab 5
  4. D. 2 cab 2

Correct answer: B. 2 cab 5

The first "cab" starts at index 2. p + 3 is index 5, which prints "cab" to the end. Searching again from p + 1 skips the first match and finds the next "cab" at index 5. Searching from p itself would find index 2 again.

More C Programming questions

Exams that ask this topic