ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
int len(const char *s) { return *s ? 1 + len(s + 1) : 0; }
int cnt(const char *s, char c) { return !*s ? 0 : (*s == c) + cnt(s + 1, c); }
int main(void) {
    const char *w = "mississippi";
    printf("%d %d", len(w), cnt(w, 's') * cnt(w, 'p'));
}
  1. A. 11 8
  2. B. 12 8
  3. C. 11 12
  4. D. 11 6

Correct answer: A. 11 8

len counts characters up to the '\0': 11 (not 12, which would count the terminator). (*s == c) is 1 or 0, so cnt adds up matches: 4 s's and 2 p's, product 8.

More C Programming questions

Exams that ask this topic