>Why are you bothering to even manipulate the string? On many compilers, 
>the code will "crash" (GPF) because you will be attempting to modify 
>read-only memory. 

Thomas,
I am afraid that this is not at all true for this code and in widely used 
compilers including 
gcc, borland. What you are saying is right for :

char *p="hello";
p[1]='2';  //this will crash on gcc

but, as in Anurag's code,
char p[]="hello";
p[1]='2'; //this will not at all crash !!!!

-kou.


Anuraag,

There are many better ways to solve the problem ... will post the mail later.
abt this code ...
there are huge logical errors in the code:
After second iteration of outer for loop, following is the status:
i=negative val,
j=negative val,
l=1            ***
k=2    

and hereafter l is never assigned new value. so try to fit l=strlen(s) 
somewhere in the for loop.
Also, I checked status of code after some x no of loops, j had taken some 
negative value (-74546)
where it gave segmentation fault.

Main problem is you are trying to reuse same variables for different purpose.

So it is really difficult to remove logical errors from this code. I will try 
and write a new code with same algo/logic
and post it.

-kou.


----- Original Message ----
From: Thomas Hruska <[EMAIL PROTECTED]>
To: [email protected]
Sent: Thursday, 23 August, 2007 7:29:08 AM
Subject: Re: [c-prog] decomress a string "a2b3c4" to aaaabbbcccc

anuraag dimri wrote:
> Hi there,
> i tried sumtyn like decompressing a4b7c2 in to aaaabbbbbbbcc. ..didnt 
> worked... i'm storing it in an array of big size and then trying to first 
> shift b7c2 and then expand a4..is it acceptable.. heres the code...the 
> numbers i have assumed to be single digit numbers in the original string.
> 
> 
> int main()
> {
> char s[100]="a4b6c8" ;
> int i,j,k,l;
> l=strlen(s);
> 
> for(i=j=0; s[i]!='\0';i+ +)
> {
> if(s[i] >= 'a' && s[i] <= 'z')
> s[j++]=s[i]; 
> 
> if(s[i] >= '0' && s[i] <= '9')
> {
> k = s[i]-48;
> while(i<l)
> {
> s[l+(k-2)]=s[ l];
> l--;
> }
> s[l+(k-1)]=' \0'; /*marking the new end of string*/
> }
> 
> while((k-2)> 0)
> {/*some problem here may b*/
> s[--j]=s[--i] ; 
> k--;
> }
> 
> }
> s[j]='\0';
> printf("\n%s" ,s);
> }

Just so you know, pretty much every viable compression algorithm looks 
at the bitstream for patterns...usually of the repeating variety. Your 
method is what is known as Run Length Encoding (RLE) but somewhat more 
basic. RLE is generally considered to be a very weak compression 
algorithm. PCX was the only widely used file format to use RLE (AFAIK).

Why are you bothering to even manipulate the string? On many compilers, 
the code will "crash" (GPF) because you will be attempting to modify 
read-only memory. Also, assuming you have enough memory for an 
operation on the stack involving strings is generally a bad idea. 
Calculate how much memory you need, allocate it, and then use the 
allocated space for the expanded data.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task. Stay on task.

http://www.CubicleS oft.com/MyTaskFo cus/





      Once upon a time there was 1 GB storage in your inbox. To know the happy 
ending go to http://help.yahoo.com/l/in/yahoo/mail/yahoomail/tools/tools-08.html

[Non-text portions of this message have been removed]

Reply via email to