Run-length encoding using C Recently I was asked in an interview to convert the string say "aabbbccccddddd" to "a2b3c4d5". i.e we have to avoid repeating same character and just adding the repeat count. Here 'a' is repeated twice in the input and so we have to write it as 'a2' in the output. Also i have to write a function to reverse the format (i.e from the string "a2b3c4d5" to "aabbbccccddddd") back to the original one. I was free to use either C or C++ language. I have tried the below code. But the interviewer seems to be not very happy with this. He asked me to try some smart way than this.

In the below code, I used formatstring() to eliminate repeated chars by just adding the repeated count and used reverseformatstring() to convert back to the original string.

formatstring()

void formatstring(char* target, const char* source)
{
    int charRepeatCount = 1; bool isFirstChar = true;
    while (*source != '\0')
    {
        if (isFirstChar)
        {
            //Whatever be the first character, we have to add it to the target
            isFirstChar = false;
            *target = *source;
            source++; target++;
        }else
        {
            if (*source == *(source-1))
            {
                //Comparing the current char with previous one and incrementing repeat count
                charRepeatCount++;
                source++;
            }else
            {
                if (charRepeatCount &gt; 1)
                {
                    //Converting repeat count to string and appending it to the target
                    char repeatStr[10];
                    _snprintf(repeatStr, 10, "%i", charRepeatCount);
                    int repeatCount = strlen(repeatStr);
                    for (int i = 0; i &lt; repeatCount; i++)
                    {
                        *target = repeatStr[i];
                        target++;
                    }
                    charRepeatCount = 1; //Reseting repeat count
                }
                *target = *source;
                source++; target++;
            }
        }
    }
    if (charRepeatCount &gt; 1)
    {
        //Converting repeat count to string and appending it to the target
        char repeatStr[10];
        _snprintf(repeatStr, 10, "%i", charRepeatCount);
        int repeatCount = strlen(repeatStr);
        for (int i = 0; i &lt; repeatCount; i++)
        {
            *target = repeatStr[i];
            target++;
        }
    }
    *target = '\0';
}


reverseformatstring()

void reverseformatstring(char* target, const char* source)
{
    int charRepeatCount = 0; bool isFirstChar = true;
    while (*source != '\0')
    {
        if (isFirstChar)
        {
            //Whatever be the first character, we have to add it to the target
            isFirstChar = false;
            *target = *source;
            source++; target++;
        }else
        {
            if (isalpha(*source))
            {
                //If current char is alphabet, we can simply add it to the target
                *target = *source;
                target++; source++;
            }
            else
            {
                while (isdigit(*source)) //Getting repeat count of previous character
                {
                    int currentDigit = (*source) - '0';
                    charRepeatCount = (charRepeatCount == 0) ? currentDigit : (charRepeatCount * 10 + currentDigit);
                    source++;
                }
                //Decrement repeat count as we have already written first unique char to the target
                charRepeatCount--;
                while (charRepeatCount &gt; 0) //Repeating the last char for this count
                {
                    *target = *(target - 1);
                    target++; charRepeatCount--;
                }
            }
        }
    }
    *target = '\0';
}


I didn't find any issues with the above one. Is there any better way of doing the same?
Did you ask any questions before you started?
It seems like there are a couple of holes in the design that need firming up.

Is there a maximum run length?  (ie is single digit lengths enough)
Does the input string only contain alpha? (ie no numbers).
Do you encode single character runs? (ababab =&gt; a1b1a1b1a1b1 or ababab)

General comments
The algorithm you are using is too complex.
Some of the answers on SO show you how to simplify the algorithm so I will not repeat that here (they are all good).
Code comments
Don't put two variables on a line:
int charRepeatCount = 0; bool isFirstChar = true;

Any function that begins with an underscore is a not a good idea.
_snprintf(repeatStr, 10, &quot;%i&quot;, charRepeatCount);

The underscore means that it is implementation defined. If there are no alternatives fine. But always prefer standard functions to non-standard implementations hacks (especially if they were done by MS). In this specific case the standard one is better designed and make usage easier.
Also why are you printing to a character array then copying to the destination array?
 _snprintf(repeatStr, 10, &quot;%i&quot;, charRepeatCount);
 int repeatCount = strlen(repeatStr);
 for (int i = 0; i &lt; repeatCount; i++)
 {
     *target = repeatStr[i];
     target++;
 }

You can print directly to the destination:
 sprintf(target, &quot;%i&quot;, charRepeatCount);  // notice not snprintf because I
                                          // I don't know the size of the
                                          // target. That is comming up.
                                          // You should definately use the
                                          // snprintf() version if you can.

You have sections of repeated code (the bit above).
If you are repeating code you should break it into a separate function.
Interface design:
void formatstring(char* target, const char* source)

I have no idea how much space I am allowed to use in target. Either allow the function to allocate its own space (and return the pointer) or get a maximum size passed into the function. If you pass in a size the return value is usually the amount of space you would have needed to succeed. This allows the user to do pre-flighting to work out the size required.
int size = LokiFormatString(source, NULL, 0);
char* tareget = (char*) malloc(size + 1);
int size = LokiFormatString(source, target, size);