ProgrammingHardProduct company OA
What is printed?
What is printed?
const char *s = "a1b22c333";
int sum = 0, cur = 0;
for (; *s; s++) {
if (*s >= '0' && *s <= '9') cur = cur * 10 + (*s - '0');
else { sum += cur; cur = 0; }
}
sum += cur;
printf("%d", sum);- A. 14
- B. 333
- C. 356
- D. 23
Correct answer: C. 356
Digits accumulate into a number until a letter ends it: 1, 22 and 333. The last number is added by the line after the loop, so the sum is 356. '14' adds digits one by one; '23' forgets the final sum += cur.
More C Programming questions
- What is printed? c typedef int (Op)(int); int twice(int x) { return 2 x; } int plus3(int x) { return x + 3; } int apply(Op f, Op g, int x)…
- What is printed? c for (int k = 0; k < 3; k++) { static int s[3]; int a[3] = {0}; s[k] = a[k] = k + 1; printf("%d%d ", s[0] + s[1] + s[2]…
- Assume ASCII. What is printed? c const char s = "pointer"; printf("%c%c%c", s + 1, s[s - 'o' + 2], (s + strlen(s) - 1) - 1);
- With int a = 3, b = 7; which of these statements compile as C (not C++)? c (a > b ? &a : &b) = 0; / (i) / (a > b ? a : b) = 0; / (ii) / a…
- What is printed? c int f(int n) { static int depth = 0; depth++; if (n <= 1) return depth; int a = f(n - 1); int b = f(n - 2); return a +…
- What is printed? c char s[] = "hello world"; char p = strchr(s, 'o'), q = strrchr(s, 'o'); q = '0'; printf("%s %zu %td", s, strlen(p), q…