ProgrammingHardProduct company OA

What is printed?

What is printed?
char s[] = "hello world";
char *p = strchr(s, 'o'), *q = strrchr(s, 'o');
*q = '\0';
printf("%s %zu %td", s, strlen(p), q - p);
  1. A. hello 3 3
  2. B. hello w 3 3
  3. C. hello w 7 3
  4. D. hello world 7 3

Correct answer: B. hello w 3 3

strchr finds the first 'o' (index 4) and strrchr the last (index 7). Writing '\0' at index 7 cuts s to "hello w", and the string from p is now "o w", length 3. q − p = 3. '7' measures p as if the string had not been cut.

More C Programming questions

Exams that ask this topic