ProgrammingHardInfosys · Wipro

What is printed?

What is printed?
#include <stdio.h>
#include <string.h>
int main(void) {
    char s[] = "stressed";
    char *a = s, *b = s + 7;
    while (a < b) {
        char t = *a;
        *a++ = *b;
        *b-- = t;
    }
    printf("%s", s);
}
  1. A. desserts
  2. B. stressed
  3. C. dessrets
  4. D. dessert

Correct answer: A. desserts

The two pointers swap characters from both ends of the 8-character string, so the result is desserts. The pointer on the left must move after each swap as well, or characters are overwritten.

More C Programming questions

Exams that ask this topic