Thread: Need help-decompression string

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    5

    Need help-decompression string

    hi,
    Im trying to write a function that decompresses a string and returns the original string.
    for example:
    p3B2c4 will be pppBBcccc
    this is what I have tried to do, the function returns nothing.
    pls tell me where I'm wrong.
    Code:
    char *uncompress(char *coded_string)
    {
    	int size=0, i, j;
    	char *original = (char *)malloc(size*sizeof(char));
    	if (original == NULL)
    	{
    		printf("Dynamic allocation failed in uncompress\n");
    
    
    		return NULL;
    	}
    	for (i = 0; coded_string[i] != '\0'; i++)
    	{
    		if (coded_string[i] >=1)
    			
    		size = size + coded_string[i] + 1;
    
    
    
    
    	}
    	for (j = 0; coded_string[j] != '\0'; j++)
    	{
    		if ((j % 2 )> 0)
    		{
    			original[j--] = coded_string[j];
    		}
    	}
    	free(original);
    	return original;
    }

  2. #2
    Registered User
    Join Date
    May 2016
    Posts
    2
    When you step into the for(j=0...
    Then j%2 has R=0 so 0!>0 so it doesnt step in to that if

    is that meant to be? I would recommend some comments so i know what ur code is doing since u arnt self documenting

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You need to analyse the string to come up with the right size before you call malloc(). It does no good to call malloc() with a size of zero and then expect it to grow.

    I know you tried to do that, but it's easier to explain the other problems just starting over.

    There's no place where you look for a digit character and respond based on that. It's a really important part to doing it right. You can figure out the size of your uncompressed string logically: "p3B2c4 " 3 + 2 + 4 = 9 characters long... "pppBBcccc" = 9 characters long.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PKWare compression/decompression
    By TAZIN in forum General Discussions
    Replies: 11
    Last Post: 03-05-2012, 03:09 PM
  2. Targa Decompression
    By carrotcake1029 in forum Tech Board
    Replies: 3
    Last Post: 07-17-2008, 12:48 AM
  3. Compression/Decompression Wave File and MP3
    By cindy_16051988 in forum Projects and Job Recruitment
    Replies: 51
    Last Post: 04-29-2006, 06:25 AM
  4. Reverse Decompression.
    By Coder87C in forum C++ Programming
    Replies: 2
    Last Post: 04-11-2005, 04:50 PM
  5. Video Decompression
    By *Michelle* in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 06-27-2002, 05:47 PM

Tags for this Thread