ProgrammingVery hardProduct company OA

What is printed?

What is printed?
void rev(char *s, int i, int j) {
    if (i >= j) return;
    char t = s[i]; s[i] = s[j]; s[j] = t;
    rev(s, i + 2, j - 2);
}
int main(void) {
    char s[] = "ABCDEFGH";
    rev(s, 0, 7);
    printf("%s", s);
}
  1. A. HBFDECGA
  2. B. HBFDEGCA
  3. C. HBCDEFGA
  4. D. HGFEDCBA

Correct answer: A. HBFDECGA

The steps of 2 swap only (0, 7) and (2, 5); the next call has i = 4, j = 3 and stops. So A↔H and C↔F: H B F D E C G A. 'HGFEDCBA' is a full reversal with steps of 1.

More C Programming questions

Exams that ask this topic