ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
const char *s = "mississippi";
printf("%td %td %td", strchr(s, 's') - s, strrchr(s, 's') - s, strstr(s, "ssip") - s);
  1. A. 2 6 2
  2. B. 2 6 5
  3. C. 3 7 6
  4. D. 2 3 5

Correct answer: B. 2 6 5

Indices: m0 i1 s2 s3 i4 s5 s6 i7 p8 p9 i10. strchr finds the FIRST 's' (index 2) and strrchr the LAST (index 6). strstr returns a pointer to the first occurrence of the whole substring "ssip", which begins at index 5 ("ssippi"); the earlier "ss" at 2 is followed by 'i','s', not 'i','p'. Subtracting s turns each pointer into an index.

More C Programming questions

Exams that ask this topic