ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
int cnt[26] = {0};
for (const char *p = "banana"; *p; p++) cnt[*p - 'a']++;
int best = 0;
for (int i = 1; i < 26; i++) if (cnt[i] > cnt[best]) best = i;
printf("%c%d", 'a' + best, cnt[best]);
  1. A. a6
  2. B. b1
  3. C. a3
  4. D. n2

Correct answer: C. a3

Subtracting 'a' turns each letter into an index 0 to 25 (valid because the lower-case letters are consecutive in ASCII). banana has a × 3, n × 2, b × 1, so the most frequent letter is a with count 3.

More C Programming questions

Exams that ask this topic