ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
int pal(const char *s, int i, int j) {
    if (i >= j) return 1;
    if (s[i] != s[j]) return 0;
    return pal(s, i + 1, j - 1);
}
int main(void) {
    const char *w[] = {"level", "abca", "a", "noon", "abcab"};
    int c = 0;
    for (int k = 0; k < 5; k++) c += pal(w[k], 0, (int)strlen(w[k]) - 1);
    printf("%d", c);
}
  1. A. 3
  2. B. 2
  3. C. 5
  4. D. 4

Correct answer: A. 3

level, a and noon are palindromes (a single letter hits i >= j at once). abca fails at b vs c, and abcab fails at once because 'a' ≠ 'b'. So 3. '4' miscounts abca, which matches only at its ends.

More C Programming questions

Exams that ask this topic