ProgrammingVery hardProduct company OA

What is printed?

What is printed?
void rev(char *a, char *b) { while (a < b) { char t = *a; *a++ = *b; *b-- = t; } }
int main(void) {
    char s[] = "abc de";
    rev(s, s + 5);
    rev(s, s + 1);
    rev(s + 3, s + 5);
    printf("%s", s);
}
  1. A. abc de
  2. B. de abc
  3. C. de cba
  4. D. ed cba

Correct answer: B. de abc

This is the reverse-the-words trick. Reversing the whole string gives "ed cba"; reversing the first word (positions 0 to 1) gives "de cba"; reversing the second (positions 3 to 5) gives "de abc". The word order has swapped while each word reads normally.

More C Programming questions

Exams that ask this topic