ProgrammingHardAccenture · Cognizant pattern

What is printed?

What is printed?
char s[] = "abcdef";
char *p = s, *q = s + strlen(s) - 1;
while (p < q)
    *p++ = *q--;
printf("%s", s);
  1. A. fedcba
  2. B. fedabc
  3. C. feddef
  4. D. abccba

Correct answer: C. feddef

There is no temporary, so this copies instead of swapping: s[0] = 'f', s[1] = 'e', s[2] = 'd', then p = 3 and q = 2 stop the loop. The back half keeps d e f, giving feddef.

More C Programming questions

Exams that ask this topic