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);- A. xyzDE
- B. {|}AB
- C. abcAB
- 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
- What is printed? c int up(int a, int b) { return (a + b - 1) / b; } int main(void) { printf("%d %d %d", up(10, 3), up(9, 3), up(0, 3)); }
- What is printed? c int t = 0; for (unsigned i = 0; i < 16; i++) for (unsigned x = i; x; x &= x - 1) t++; printf("%d", t);
- What is printed? c void s1(int a, int b) { int t = a; a = b; b = t; } void s2(int a, int b) { int t = a; a = b; b = t; } int main(void) {…
- Why does this fail to compile in C? c void f(int n) { static int k = n; }
- Which line correctly allocates space for a copy of a string pointed to by const char s?
- What is printed? c int a = 3, b = 0; int r = (a > b) + (a && b) 2 + (a || b) 4 + !b 8; printf("%d", r);