I gave it a try ....
Better code (with same algo) ... it works !!!

//in place decompression of a string.
//freq<10
main ()
{
        char str[100]="a4b6c2d7g9";
        int i,freq,len,j,k,curr_char,room;
        printf ("\n%s\n",str);
        for(i=0;str[i]!=0;)
        {
                if(str[i]>='a' && str[i]<='z')
                {
                        i++;
                        freq=str[i]-48; //convert next digit to integer.
                        room=freq-2;
                        len=strlen(str);
                        for(j=len; j>i ;j--)    //shift the string to right and 
make room for expansion.
                        {
                                str[j+room]=str[j];
                        }
                        printf ("\n%s\n",str);
                        j=i;
                        curr_char=str[i-1];
                        for(k=0;k<=room;k++)    //expand
                        {
                                str[j]=curr_char;
                                j++;
                                i++;
                        }
                        printf ("\n%s\n",str);
                }
                else
                {
                        //error !!!
                }
        }
        // and done !!! :)
        printf ("\n%s\n",str);

}



----- Original Message ----
From: Thomas Hruska <[EMAIL PROTECTED]>
To: [email protected]
Sent: Saturday, 25 August, 2007 5:13:41 AM
Subject: Re: [c-prog] decomress a string "a2b3c4" to aaaabbbcccc

David wrote:
> Thomas Hruska a écrit :
>> kou ksk wrote:
>> 
>>>> 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.
>>> 
>> Tempting fate is always a bad idea. From my perspective, both are 
>> identical and interchangeable. An overzealous optimizing compiler (e.g. 
>> Intel*) may think the same way.
>>
>> * Intel's optimizing compiler suite couldn't/can' t be used, for 
>> instance, to compile the Linux kernel. (Or at least it used to not be 
>> able to because it was overzealous in its optimizations - at least that 
>> was the explanation I received).
>>
>> 
> 
> I don't know much C,
> But can you clarified this : "both are identical" ?
> I am not sure to understand.
> 
> I don't have intel icc compiler to see assembly generated by both code But it 
> could be interested to see what will be generated by (two initialization with 
> the same literal c-string)
> 
> char* q = "abcdef"; // could be (as with aCC) a warning for C++
> char* p = "abcdef";
> 
> And 
> 
> char q[] = "abcdef";
> char p[] = "abcdef";
> 
> David

I don't have access to the Intel compiler (but I'd love to try it 
sometime and see if it is everything it claims to be).

My guess (without actually trying it) is the assembler generated for the 
above is most likely _identical_. The difference is going to be where 
in the final executable the compiler decides to dump the strings 
(read-only or read/write memory).

-- 
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/





      Why delete messages? Unlimited storage is just a click away. 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