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);- A. 2
- B. 4
- C. 11
- 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
- What is printed? c 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 ==…
- What is printed? c char src[] = "abc", dst[8]; char d = dst; const char s = src; while ((d++ = s++)); printf("%s %td %td", dst, d - dst, s…
- What does calloc(n, size) guarantee that malloc(n size) does not?
- What is printed? c int a[3][3] = {{1}, {2, 3}, [2][1] = 7}; printf("%d %d %d %d", a[0][1], a[1][1], a[2][0], a[2][1]);
- What does f(6) print? c void f(int n) { printf("%d", n); if (n > 1) f(n / 2); printf("%d", n); }
- What is printed? c sizet a = 3, b = 5; if (a - b > 0) puts("bigger"); else puts("smaller");