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;
}

first I would create an array of 126 integers because there are at most 126 possible characters on the standard keyboard. Initialize all ints to 0. Then loop through the array and use each character as an intex into the array so that its element can be incremented. When done, only the array elements whose value is greater than zero were in the original string.

For example

int array[126] = {0};
//
// increment an array element
++array['a'];

// loop through the original string
int i;
char str[] = "aabbbbcccdddd";
for(i = 0; str[i]  != NULL; i++)
   ++array[str[i]);
// now you should be able to figure out the rest
// of the assignment

the thing is that i got to use pointers , does any1 know why my code is not working ?
well ive done some edites to the code but still nothing.

#include <stdio.h>
void compact(char *source, char *dest)
{
 char var = *source;
 int counter=0;
 while (*source)
 {
  if (var == *source)
  {
   counter++;
   source++;
  }
  else
  {
   *dest = counter + '0';
   *(dest+1) = var;
   dest++;
  }
  var = *source;
  counter=0;
 }
 *dest = '\0';
}
int main(void)
{
 char strsource[40],strdest[20];
 printf ("String: ");
 gets (strsource);
 compact(strsource,strdest);
 puts (strdest);
 return 0;
}

its not working because you need to use a lot more counters. using only one counter what do you do when the characters in the string are in random order?

<< *dest = counter + '0';
That will not work when the value of counter is greater than 9 because 10 + '0' is 10 + 48 = 58, or the ascii value for a colon (see an ascii chart) But it will be ok if your assignment limits the number of allowed characters to less than 10.

>> source++;
move that line down just below the end of the if statement because source needs to be incremented at each iteration of the while loop.

>> counter=0;
move that line up under the else statemnet. Only want to zero it out then the letter changes.

commented: Very good +1

Finally i got it.
btw; the characters should be in a random order and i only need the use of one counter;

#include <stdio.h>
void Compact(char *source,char *dest);
int main(void)
{
 char str1[40],str2[40];
 printf ("String << ");
 gets (str1);
 Compact(str1,str2);
 printf ("String >> ");
 puts (str2);
 return 0;
}
void Compact (char *source,char *dest)
{
 char var = *source;
 int counter = 0;
 while (*source)
 {
  if (var == *source)
  {
   counter++;
   source++;
  }
  if (var != *source || !*source)
  {
   *dest = counter + '0';
   dest++;
   *dest = var;
   dest++;
   var = *source;
   counter = 0;
  }
 }
 *dest = '\0';
}

>>btw; the characters should be in a random order
sorry but your program does not work with a string like this: "abcabcdeab". That is what is meant by random order. But congratulations on finding your own solution :)

Not to forget that his scheme will work only for characters whose count is less than or equal to 9. Any higher and the algo is busted.

@OP Maybe you should try out [search]sprintf[/search] instead of adding plain ascii characters.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.