ProgrammingHardProduct company OA
What is printed?
What is printed?
int rot(const char *a, const char *b) {
char buf[32];
if (strlen(a) != strlen(b)) return 0;
strcpy(buf, a);
strcat(buf, a);
return strstr(buf, b) != NULL;
}
int main(void) {
printf("%d%d%d", rot("abcde", "cdeab"), rot("abcde", "abced"), rot("aa", "a"));
}- A. 100
- B. 111
- C. 110
- D. 101
Correct answer: A. 100
Every rotation of a appears inside a + a, so b is a rotation exactly when the lengths match and b occurs in the doubled string. cdeab is in abcdeabcde; abced is not; "a" occurs in "aaaa" but fails the length check, which is what that check is for.
More C Programming questions
- What is printed? c int alt(unsigned x) { unsigned y = x ^ (x >> 1); return (y & (y + 1)) == 0; } int main(void) { printf("%d%d%d", alt(5)…
- What is printed? c struct In { int a[2]; }; struct Out { struct In in[2]; int k; } o = {{{{1, 2}}, {{3, 4}}}, 5}; printf("%d %d"…
- What is printed? c struct In { int a[2]; }; struct Out { struct In in[2]; int k; } q = {1, 2, 3, 4, 5}; printf("%d %d", q.in[1].a[1], q.k);
- What is printed? c int c = 0; int f(int n) { c++; return n <= 1 ? 1 : f(n / 2) + f(n - n / 2); } int main(void) { int r = f(7); printf("%d…
- Which of these loops run their body exactly for i = 4, 3, 2, 1, 0 (once each) with an unsigned i? c for (unsigned i = 5; i-- > 0; ) { /…
- What is printed? c int x = -1; unsigned y = 1; printf("%d", (x < 0 ? x : y) < 0);