ProgrammingVery hardProduct company OA
What is printed?
What is printed?
void rev(char *a, char *b) { while (a < b) { char t = *a; *a++ = *b; *b-- = t; } }
int main(void) {
char s[] = "abc de";
rev(s, s + 5);
rev(s, s + 1);
rev(s + 3, s + 5);
printf("%s", s);
}- A. abc de
- B. de abc
- C. de cba
- D. ed cba
Correct answer: B. de abc
This is the reverse-the-words trick. Reversing the whole string gives "ed cba"; reversing the first word (positions 0 to 1) gives "de cba"; reversing the second (positions 3 to 5) gives "de abc". The word order has swapped while each word reads normally.
More C Programming questions
- What is printed? c int n = 0; int t(void) { n++; return 1; } int f(void) { n++; return 0; } int main(void) { int r = f() || t() && f() ||…
- What is printed? c int a[] = {3, 5, 0, 7}, i = 0, s = 0, v; while ((v = a[i++])) s += v; printf("%d %d", s, i);
- What is printed? c int cnt[26] = {0}; for (const char p = "banana"; p; p++) cnt[p - 'a']++; int best = 0; for (int i = 1; i < 26; i++) if…
- In a binary search over int indices, why is mid = lo + (hi - lo) / 2 preferred to mid = (lo + hi) / 2?
- What is printed? c int A[2][2] = {{1, 2}, {3, 4}}, B[2][2] = {{0, 1}, {1, 0}}, C[2][2] = {0}; for (int i = 0; i < 2; i++) for (int j = 0…
- When should a variable be declared volatile?