ProgrammingHardTCS NQT · Infosys

Assume ASCII. What is printed?

Assume ASCII. What is printed?
char s[] = "xyzAB";
for (char *p = s; *p; p++)
    if (*p >= 'a' && *p <= 'z') *p = 'a' + (*p - 'a' + 3) % 26;
printf("%s", s);
  1. A. xyzDE
  2. B. {|}AB
  3. C. abcAB
  4. D. abcDE

Correct answer: C. abcAB

Only lower-case letters are shifted, by 3 with wrap-around: x → a, y → b, z → c. The % 26 is what prevents running into '{', '|' and '}' after 'z'. Capital letters fail the test and stay as they are.

More C Programming questions

Exams that ask this topic