Skip to main content
fixed a mistake
Source Link
Alberto
  • 728
  • 4
  • 20

I tried to solve your question quickly and this is my code:

#include <stdio.h>

#define SIZE 255

int main()
{
  char input[SIZE] = "aabbcccd";/*input string*/
  char output[SIZE]={'\0'};/*where output string is stored*/
  char seen[SIZE]={'\0'};/*store all chars already counted*/
  char *ip = input;/*input pointer=ip*/
  char *op = output;/*output pointer = op*/
  char *sp = seen;/*seen pointer=sp*/
  char c,count;
  int i,j,done;

  i=0;
  while(i<SIZE && input[i]!='\0')
  {
    c=input[i];
    //don't count if already searched:
    done=0;
    j=0;
    while(j<SIZE)
    {
      if(c==seen[j])
      {
         done=1;
         break;
      }
      j++;
    }
    if(done==0)
    {//if i never searched char 'c':
      *sp=c;
      sp++;
      *sp='\0';
      sp++;
      //count how many "c" there are into input array:
      count = '0';
      j=0;
      while(j<SIZE)
      {
         if(ip[j]==c)
         {
        count++;
         }
     j++;
      }
      *op=c;
      op++;
      *op=count;
      op++;
    }
    i++;
  }

  *op='\0';
  printf("input: %s\n",input);
  printf("output: %s\n",output);

  return 0;
}

It's not a good code for several reasons(I don't check arrays size writing new elements, I could stop searches at first empty item, and so on...) but you could think about it as a "start point" and improve it. You could take a look at standard library to copy substring elements and so on(i.e. strncpy).

I tried to solve your question quickly and this is my code:

#include <stdio.h>

#define SIZE 255

int main()
{
  char input[SIZE] = "aabbcccd";/*input string*/
  char output[SIZE]={'\0'};/*where output string is stored*/
  char seen[SIZE]={'\0'};/*store all chars already counted*/
  char *ip = input;/*input pointer=ip*/
  char *op = output;/*output pointer = op*/
  char *sp = seen;/*seen pointer=sp*/
  char c,count;
  int i,j,done;

  i=0;
  while(i<SIZE && input[i]!='\0')
  {
    c=input[i];
    //don't count if already searched:
    done=0;
    j=0;
    while(j<SIZE)
    {
      if(c==seen[j])
      {
         done=1;
         break;
      }
      j++;
    }
    if(done==0)
    {//if i never searched char 'c':
      *sp=c;
      sp++;
      *sp='\0';
      sp++;
      //count how many "c" there are into input array:
      count = '0';
      j=0;
      while(j<SIZE)
      {
         if(ip[j]==c)
         {
        count++;
         }
     j++;
      }
      *op=c;
      op++;
      *op=count;
      op++;
    }
    i++;
  }

  *op='\0';
  printf("input: %s\n",input);
  printf("output: %s\n",output);

  return 0;
}

It's not a good code for several reasons(I don't check arrays size writing new elements, I could stop searches at first empty item, and so on...) but you could think about it as a "start point" and improve it. You could take a look at standard library to copy substring elements and so on(i.e. strncpy).

I tried to solve your question quickly and this is my code:

#include <stdio.h>

#define SIZE 255

int main()
{
  char input[SIZE] = "aabbcccd";/*input string*/
  char output[SIZE]={'\0'};/*where output string is stored*/
  char seen[SIZE]={'\0'};/*store all chars already counted*/
  char *ip = input;/*input pointer=ip*/
  char *op = output;/*output pointer = op*/
  char *sp = seen;/*seen pointer=sp*/
  char c,count;
  int i,j,done;

  i=0;
  while(i<SIZE && input[i]!='\0')
  {
    c=input[i];
    //don't count if already searched:
    done=0;
    j=0;
    while(j<SIZE)
    {
      if(c==seen[j])
      {
         done=1;
         break;
      }
      j++;
    }
    if(done==0)
    {//if i never searched char 'c':
      *sp=c;
      sp++;
      *sp='\0';
      //count how many "c" there are into input array:
      count = '0';
      j=0;
      while(j<SIZE)
      {
         if(ip[j]==c)
         {
        count++;
         }
     j++;
      }
      *op=c;
      op++;
      *op=count;
      op++;
    }
    i++;
  }

  *op='\0';
  printf("input: %s\n",input);
  printf("output: %s\n",output);

  return 0;
}

It's not a good code for several reasons(I don't check arrays size writing new elements, I could stop searches at first empty item, and so on...) but you could think about it as a "start point" and improve it. You could take a look at standard library to copy substring elements and so on(i.e. strncpy).

Source Link
Alberto
  • 728
  • 4
  • 20

I tried to solve your question quickly and this is my code:

#include <stdio.h>

#define SIZE 255

int main()
{
  char input[SIZE] = "aabbcccd";/*input string*/
  char output[SIZE]={'\0'};/*where output string is stored*/
  char seen[SIZE]={'\0'};/*store all chars already counted*/
  char *ip = input;/*input pointer=ip*/
  char *op = output;/*output pointer = op*/
  char *sp = seen;/*seen pointer=sp*/
  char c,count;
  int i,j,done;

  i=0;
  while(i<SIZE && input[i]!='\0')
  {
    c=input[i];
    //don't count if already searched:
    done=0;
    j=0;
    while(j<SIZE)
    {
      if(c==seen[j])
      {
         done=1;
         break;
      }
      j++;
    }
    if(done==0)
    {//if i never searched char 'c':
      *sp=c;
      sp++;
      *sp='\0';
      sp++;
      //count how many "c" there are into input array:
      count = '0';
      j=0;
      while(j<SIZE)
      {
         if(ip[j]==c)
         {
        count++;
         }
     j++;
      }
      *op=c;
      op++;
      *op=count;
      op++;
    }
    i++;
  }

  *op='\0';
  printf("input: %s\n",input);
  printf("output: %s\n",output);

  return 0;
}

It's not a good code for several reasons(I don't check arrays size writing new elements, I could stop searches at first empty item, and so on...) but you could think about it as a "start point" and improve it. You could take a look at standard library to copy substring elements and so on(i.e. strncpy).