ProgrammingHardTCS NQT · Wipro

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    const char *s = "  a bb  ccc ";
    int words = 0, in = 0;
    for (; *s; s++) {
        if (*s == ' ') in = 0;
        else if (!in) { in = 1; words++; }
    }
    printf("%d", words);
}
  1. A. 4
  2. B. 3
  3. C. 2
  4. D. 6

Correct answer: B. 3

The state flag in marks being inside a word, so only the first letter of each run increments the count. The three words are a, bb and ccc.

More C Programming questions

Exams that ask this topic