ProgrammingVery hardProduct company OA
This prints every permutation of "abc". What is the FIFTH string printed?
This prints every permutation of "abc". What is the FIFTH string printed?
void p(char *s, int k) {
if (!s[k]) { printf("%s ", s); return; }
for (int i = k; s[i]; i++) {
char t = s[k]; s[k] = s[i]; s[i] = t;
p(s, k + 1);
t = s[k]; s[k] = s[i]; s[i] = t;
}
}
/* main: */ char s[] = "abc"; p(s, 0);- A. cab
- B. acb
- C. cba
- D. bca
Correct answer: C. cba
The swap method is not lexicographic. With a first: abc, acb. With b swapped to the front: bac, bca. With c swapped to the front (a and c exchange), the string is cba, which prints first, then cab. So the order is abc acb bac bca cba cab.
More C Programming questions
- What is printed? c int a = 1; printf("%d %d", a << 2 + 1, (a << 2) + 1 > 4 == 1);
- What is printed? c int a[] = {4, 7, 4, 9, 7, 9, 9}; int x = 0; for (int i = 0; i < 7; i++) x ^= a[i]; printf("%d", x);
- What is printed? c int a[] = {1, 2, 4, 5, 6}, x = 0; for (int i = 1; i <= 6; i++) x ^= i; for (int i = 0; i < 5; i++) x ^= a[i]…
- What is printed? c int parse(const char s) { int sign = 1, n = 0; if (s == '-') { sign = -1; s++; } while (s >= '0' && s <= '9') n = n 10…
- What is printed? c int a[] = {1, 1, 2, 3, 3, 3, 4}, k = 1; for (int i = 1; i < 7; i++) if (a[i] != a[k - 1]) a[k++] = a[i]; printf("%d…
- What is printed? c int a[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; int best = a[0], cur = a[0]; for (int i = 1; i < 9; i++) { cur = cur + a[i]…