ProgrammingHardTCS NQT · Wipro

What is printed?

What is printed?
#include <stdio.h>
int pal(const char *s, int n) {
    int l = 0, r = n - 1;
    while (l < r) {
        if (s[l] != s[r]) return 0;
        l++; r--;
    }
    return 1;
}
int main(void) {
    printf("%d", pal("racecar", 7));
}
  1. A. 0
  2. B. 2
  3. C. 1
  4. D. 3

Correct answer: C. 1

The two pointers move inward and compare matching ends. Every pair in racecar matches, so the function returns 1.

More C Programming questions

Exams that ask this topic