Thread: RLE Encoding/Decoding using simple C concept..plz help

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    4

    Exclamation RLE Encoding/Decoding using simple C concept..plz help

    Run length encoding is a compression technique that takes advantage of repeated consecutive digits. Encoding involves putting a character followed by count.

    Example input: 123334455555AAA

    Output for above input: 1\12\13\34\25\5A\3

    (1 is appearing once, 2 is appearing twice,k 3 is appearing 3 times, 4 is appearing twice, 5 is appearing 5 times, A is appearing 3 times. \ represents the ESCAPE sequence. \1 means ASCII value 1)

    If a digit is repeated more than 255 times, it should be split into two parts as in following example:

    Example input:1 repated 257 times
    Output for above input:1\2551\2

    A decoder does the reverse.

    Write a C program that encodes in RLE format and also decodes RLE format to restore to the original file. (When a file is encoded and decoded, the original file should be restored)

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    i have already done the encoding part.i want someone to help me with the decoding part..plz use simple C..i am posting the encoding part i have done for ur reference..plz help guys.its urgent..

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>
    int main()
    {
    char a[500];
    int count=0,setcount=1,temp=255;
    FILE *stream;
    clrscr();
    
    printf("\nEnter the input:\n");
    scanf("%s",&a);
    stream=fopen("g.txt","w");
    int len=strlen(a);
    printf("Original input :%s\n\nLength:%d\n\nCompressed output:",a,len);
    
    while(count<len)
    {
    	if(a[count]==a[count+1])
    	{
    	setcount++;
    	count++;
    	}
    
    	else
    		while(1)
    		{
    		if(setcount>255)
    			{
    			printf("%c\\%d",a[count],temp);
    			setcount-=temp;
    			}
    		else
    			{
    			printf("%c\\%d",a[count],setcount);
    		       fprintf(stream, " %c\\ %d", a[count],setcount);
    			setcount=1;
    			count++;
    			break;
    			}
    
    
    		}
    
    	}
    
    	printf("\n");
    
    
    	getch();
    	return(0);
    
    
    
    
    }

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> ...its urgent
    When is it due?

    gg

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Output for above input: 1\12\13\34\25\5A\3
    How do you propose to tell the difference at the start between
    1 repeated once, followed by 2, as in 12
    1 repeated 12 times followed by a \, as in 111111111111\

    The problem is, your encoding is wrong.
    You should be writing out just TWO bytes per character, one byte being the character, and the other byte representing the repeat count.

    Decoding is them much easier as you always read a pair, then expand a sequence.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    and you should work on your indentation - it is not consistent
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Agreed, the encoder is completely wrong, in so many ways!
    But since it's urgent you wont have time to understand the details so I wont bother to explain them.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    but the program is running perfectly..
    if there are 12 1's followd by 2 it will give the output as 1\122..no problem with that..thats what they have said to do in the problem..pls neone help me with the decoding part...

    input : 1\52\3 should be decoded as

    11111222

    can neone help me with a code..pls..its very urgent..thanxx guys..

  8. #8
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    His encoding, if really done in text, could work. (It could be decoded.) Example,
    Code:
    1/12/1
    Read the 1. Read the /. Now read in numbers until you get to the next /. That last number/character was just read, put it back. You now have: character / number of times to repeat.

    Granted, this encoding, if done in plain text, is horrid, and I wonder just how much benefit you'd get out of it. It should really be a binary file of two bytes: character, length. However, whatever requirements are being put on the OP may restrict that.

    EDIT: Rereading the OP's first post, I think perhaps whatever he or she has is trying to say, put it in a binary file, with byte pairs of character,length... albeit the whole thing about escape sequences is ... off.
    Last edited by Cactus_Hugger; 01-10-2008 at 12:43 AM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by garv84 View Post
    but the program is running perfectly..
    if there are 12 1's followd by 2 it will give the output as 1\122..no problem with that..thats what they have said to do in the problem..pls neone help me with the decoding part...

    input : 1\52\3 should be decoded as

    11111222

    can neone help me with a code..pls..its very urgent..thanxx guys..
    Garv, we aren't a homework writing service, no matter how urgent it may be. You show your functions code, and tell us what the problem is, and we'll do our best to give advice.

    We won't write a function, or half the program in your case, while you offer no work, at all on it.

    Cactus Hugger has described the algorithm that I would use. If the program you claim is yours, is really yours, you should have very little trouble coding it up.

    It's not that way however, because you didn't write the first program you posted. You got it from another source, and now you need help, of course.

    Programming is a lot of work. You can't become a programmer by just cherry-picking other people's code (even if it's open source or public domain or even given to you), and trying to get by. It takes a lot of work FROM YOU.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > but the program is running perfectly..
    Just because it runs without crashing and produces an output file is not a sign of perfection.

    You're not addressing the issues raised in my previous post.

    Given 1/122/3
    You need to find a way of making sure it decodes as
    1/122/3
    and not as
    1/122/3
    This is what Cactus_Hugger was talking about.

    It might be easier if you always wrote the length out as 3 digits, say
    1/0122/003
    If you know the length is always 3 digits, it makes it easier to deal with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Your description clearly indicates that you are not supposed to store character followed by backslash and integer, but a character followed by another character whose integral value indicates the number of repeats.

    So your output should look something like that:
    A0B"C^
    for 48 A-s, 34 B-s, 94 C-s
    The description uses \1 and explains how to interpret it, because characters with small ASCII values are non-printing (so you'd just see rectangles instead, for example).
    Last edited by anon; 01-10-2008 at 10:14 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    plz tell me something..

    say for ex : 1\125

    all thses are characters.we are to print 1 125 tyms.how can we conver characters 125 into integar..??

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A character is an integer, just with a small range. You don't want to print the integral value of the character at all. You want to use a char for counting, not an int.
    Code:
    char a = 's';
    char b;
    
    for (b = 0; b < a; ++b) {
    }
    Try something like that putting something inside the loop, and you might get the idea.

    You might also Google for ASCII tables.
    Last edited by anon; 01-10-2008 at 10:52 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by garv84 View Post
    plz tell me something..

    say for ex : 1\125

    all thses are characters.we are to print 1 125 tyms.how can we conver characters 125 into integar..??
    If you write the compressor properly then you don't have to!

    Everyone always teaches RLE compression by giving examples with text, and shows the compressed version as if it were text. Such examples always contain lots of repeated bytes too.
    However that's not the reality of how anybody who knows what they're doing implements it.

    You are supposed to accept binary data, and write out binary data. Every byte value between 0 and 255 should be able to be compressed from the input file, and later restored after decompression of hte compressed file. This in turn basically means that your compressed file becomes a binary format as well.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    ... and shows the compressed version as if it were text. Such examples always contain lots of repeated bytes too.
    You could of course use RLE several times over to compress you data virtually to nothing.

    Just kidding.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a simple question. plz help
    By monashhros3 in forum C Programming
    Replies: 1
    Last Post: 03-28-2008, 01:40 PM
  2. plz help me with simple string comparison.
    By MegaManZZ in forum C++ Programming
    Replies: 11
    Last Post: 02-18-2008, 01:11 PM
  3. Matrix Multiplication with simple syntax! plz help.
    By codebox in forum C Programming
    Replies: 6
    Last Post: 11-05-2004, 09:03 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM