0

I have an array of 1's and 0's which is compressed in such a way that when the number of 1's is greater than 10 it writes +n+ when n in the number of 1's and when the number of 0's is greater than 10 it writes -n- when n in the number of 0's otherwise it writes them as it is.

Now the issue is, I need to decompress the array to write it back to the file. But I can't find a way to convert the number of zeros or ones to integer. It keeps giving me an error which says initializing argument 1 of ‘int atoi(const char*) and another one on the same line which says invalid conversion from ‘char’ to ‘const char*’ I'm working in Linux. Here's a peice of my code

else if(str[i]=='+')
{
n=atoi(str[i+1]);
for(int j=0;j<n;j++)    
{
    strcat(temp,"1");
    i=i+n-1;

}           
}
2
  • atoi() is deprecated. Consider using strtol() instead. Commented Mar 30, 2013 at 14:43
  • atoi isn't deprecated. If you need an int and you're sure you're working with a nul-terminated string, there's no reason not to use atoi. Commented Mar 30, 2013 at 15:59

1 Answer 1

1

This is an algorithm do "expansion" - don't ever use it in production - for example, there is no error checking, so it is not safe. It is a quick example.

char *decode(char *q)
{
   char *all=NULL;
   long i=0;
   int n='0';
   char *p;
   if(*q== '+')
     n='1';
   ++q;
   i=strtol(q, NULL, 10);
   all=calloc( i + 1, 1);
   for(p=all; i; i--)
      *p++=n;

   return all;
}

char *decompress(char *dest, char *str)
{
    char *p=str;
    char *q=dest;
    for(; *p; p++)
    {
       if( isdigit((int)*p) )
       {
          *q++=*p;
          *q=0x0;
       }
       else  // - or +
       {
          char *tmp=decode(p);
          strcpy(q, tmp);   
          q=strchr(q, '\0');
          free(tmp);         
          p=strchr(p+1, *p);  // next 
       }
    }
    return dest;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.