I need to create a function the gets a source string pointer and a destination string pointer , so if the source string will contain -
"aaaabbbbbbcccdd"
the function will return a destination string that contains
"4a6b3c2d"
This is my code , but its not working and for some reason i can tell why
#include <stdio.h>
void compact(char *source, char *dest)
{
char var = *source;
int counter=0;
while (*source)
{
if (*source == var)
{
counter++;
source++;
}
else
{
*dest = counter + '0';
*(dest+1) = var;
dest++;
}
var = *source;
}
*dest = '\0';
}
int main(void)
{
char strsource[40],strdest[20];
printf ("String: ");
gets (strsource);
compact(strsource,strdest);
puts (strdest);
return 0;
}