ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
#include <stdio.h>
int find(const char *hay, const char *nd) {
for (const char *p = hay; *p; p++) {
const char *a = p, *b = nd;
while (*b && *a == *b) { a++; b++; }
if (!*b) return (int)(p - hay);
}
return -1;
}
int main(void) {
printf("%d", find("abcabcd", "cd"));
}- A. 2
- B. 4
- C. 5
- D. 6
Correct answer: C. 5
The search checks each start position, and the first position where cd matches is index 5, the c before d. The first c at index 2 is followed by a, so it does not match.
More C Programming questions
- What is printed? Assume int is 32 bits. c #include <stdio.h> int main(void) { unsigned a = 10; int b = -3; printf("%u", a % b); }
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 40; int c = 0; while (!(x & 1u)) { x >>= 1; c++; } printf("%d", c); }
- What is printed? c #include <stdio.h> int main(void) { int a[] = {1, 2, 2, 3, 1, 4}; int c = 0; for (int i = 0; i < 5; i++) if (a[i] < a[i…
- For uint32t a = 5 and b = 3, how many of these statements are true? (i) a - b > a (ii) (a - b) + b == a (iii) ~a == UINT32MAX (iv) a / b b…
- What is printed? c #include <stdio.h> int main(void) { int a[3][4]; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) a[i][j] = i 4…
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { unsigned x = 0xABCD; printf("%u", (x >> 5) & 7u); }