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"));
}
  1. A. 100
  2. B. 111
  3. C. 110
  4. 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

Exams that ask this topic