ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
const char *s = "  hi  there   you ";
int words = 0, in = 0;
for (; *s; s++) {
    if (*s == ' ') in = 0;
    else if (!in) { in = 1; words++; }
}
printf("%d", words);
  1. A. 2
  2. B. 4
  3. C. 11
  4. D. 3

Correct answer: D. 3

The flag in records whether we are inside a word; a word is counted only at its first character after a space. Runs of spaces, and spaces at either end, therefore add nothing: hi, there, you = 3.

More C Programming questions

Exams that ask this topic