ProgrammingVery hardProduct company OA
What is printed?
What is printed?
char s[] = "aaabccdddd";
char *r = s, *w = s;
while (*r) {
char c = *r; int n = 0;
while (*r == c) { r++; n++; }
*w++ = c;
if (n > 1) *w++ = (char)('0' + n);
}
*w = '\0';
printf("%s %td", s, w - s);- A. a3bc2d4 7
- B. a3b1c2d4 8
- C. a3bc2d4 10
- D. abcd 4
Correct answer: A. a3bc2d4 7
A read pointer r scans runs and a write pointer w compresses in place; w can never overtake r because a run of length n writes at most min(n, 2) characters. Runs: aaa → 'a3', b → 'b' (no count for a single), cc → 'c2', dddd → 'd4'. The explicit terminator at w cuts off the stale tail, so the string is a3bc2d4 and w − s = 7. The inner loop stops at '\0' on its own because '\0' never equals c.
More C Programming questions
- Assume 32-bit unsigned int. What is printed? c unsigned int x = 0x5Cu; / 0101 1100 / unsigned int low = x & -x; unsigned int y = x + low…
- Assume 4-byte int with 4-byte alignment. What is printed? c struct P { char tag; int v; }; struct P a[4] = {{'a', 1}, {'b', 2}, {'c', 3}…
- Assume plain char is signed 8-bit, two's complement, and EOF is −1. What is printed? c char c = 'xff'; printf("%d %d %d %d", c == 0xFF…
- Read the declaration: c char ((x)[4])(int); What is x?
- What is printed? c char a[8] = "abcdefg"; memmove(a + 2, a, 4); char b[6] = "XXXXX"; strncpy(b, "hi", 4); char c[4]; strncpy(c, "wxyz"…
- What is printed? c struct N { int v; struct N next; }; struct N n[5] = {{1, n + 1}, {2, n + 2}, {3, n + 3}, {4, n + 4}, {5, 0}}; struct N…