ProgrammingHardProduct company OA

What is printed?

What is printed?
int sub(const char *a, const char *b) {   /* is a a subsequence of b? */
    if (!*a) return 1;
    if (!*b) return 0;
    return sub(*a == *b ? a + 1 : a, b + 1);
}
int main(void) { printf("%d%d%d", sub("ace", "abcde"), sub("aec", "abcde"), sub("", "x")); }
  1. A. 101
  2. B. 111
  3. C. 100
  4. D. 001

Correct answer: A. 101

The function walks b and advances in a only on a match, so it checks whether a's letters appear in b in order. a, c, e do; for aec, after a and e there is no c left. The empty string is a subsequence of anything.

More C Programming questions

Exams that ask this topic