It's not hard if all the run lengths are at least 2. void decode(char *buf, int in_size, int buf_size) { int i, j, rl, p; char t;
// Reverse the input. for (i = 0, j = in_size - 1; i < j; i++, j--) { t = buf[i]; buf[i] = buf[j]; buf[j] = t; } // Copy to end of buffer (carefully) for (i = in_size - 1, j = buf_size - 1; i >= 0; i--, j--) buf[j] = buf[i]; // Execute commands. ++j; // j now points to first command for (i = p = 0; i < in_size; i += 2, j += 2) { sscanf(&buf[j], "%d%c", &rl, &t) != 2); while (rl-- > 0) buf[p++] = t; } } If they can be rl 1, I don't think it can be done without a trick like setting the high bit of the character instead of storing the run length of 1 or adding the run lengths to the char values so both are stored in one location. On Mar 19, 1:08 pm, ATul SIngh <atulsingh7...@gmail.com> wrote: > This was a MS question asked recently on Run length Decoding. I was > given > Input- a3b5c3d2 > > And the output should be ddcccbbbbbaaa > > Assuming that the memory given is sufficient to accomodate the whole > string. > And this conversion should be inplace. ie the output string should not > use another array. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.