On 8/27/07, kou ksk <[EMAIL PROTECTED]> wrote:
>
> great !!
>
> I realized that just after i posted the code ..
> one more thing ... is it really necessary to use compression when
> frequency=1,2 ??
> for 1, it will be overhead, for 2, it won't get compressed :)
>
> btw thanks for the reply  :)
>
> -kou


I agree with you. As far as I  am concerned, it isn't necessary to use
compression for frequency=1 or 2. But I put in the checks anyway just to be
thorough. The program would fail to operate correctly otherwise.

~Rick

----- Original Message ----
> From: Rick <[EMAIL PROTECTED]>
> To: [email protected]
> Sent: Monday, 27 August, 2007 9:36:33 PM
> Subject: Re: [c-prog] decomress a string "a2b3c4" to aaaabbbcccc
>
>
> At 8/24/2007 08:14 PM, you wrote:
> >I gave it a try ....
> >Better code (with same algo) ... it works !!!
> >
> >//in place decompression of a string.
> >//freq<10
> >main ()
> >{
> > char str[100]="a4b6c2d7g 9";
> > 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) ;
> >
> >}
>
> It works with the supplied data. But if the
> increment count is only one, it fails. I've fixed
> that, and also checked for invalid entries (i.e.
> an entry must consist of a letter/number
> combination. Upper case letters are allowed.).
>
> To more easily check different combinations, I
> let the user enter the entry pairs.
>
> ~Rick
>
> #include <cstdlib>
> #include <iostream>
>
> using namespace std;
>
> int main(int argc, char *argv[])
> {
>
> // in-place decompression of a string.
> // freq < 10
>
> char str[100]="a4b6c2d7g 9";
> int i,freq,len,j, k,curr_char, room;
> int err = 0;
>
> cout << "Enter letter/count combinations: ";
> cin >> str;
> cout << "You entered " << str << endl;
>
> for (i = 0; str[i] != 0;)
> {
> if (err) // Shift the string left 2
> positions to delete the bad entry
> {
> k = i; // Point k to current entry
> i -= 2; // Decrement i to overwrite bad entry
> j = i; // Point j to new entry point
> cout << endl << "Removing bad entry ["
> << str[i] << str[i+1] << "]" << endl;
> do
> {
> str[j++] = str[k++];
> } while (str[k]);
> str[j] = '\0'; // Terminate the string
> cout << str << endl;
> err = 0;
> }
> if ( (str[i] >= 'a' && str[i] <= 'z')
> || (str[i] >= 'A' && str[i] <= 'Z') )
> {
> i++;
> if (str[i] >= '0' && str[i] <= '9')
> {
> freq=str[i]- 48; // Convert next digit to integer.
> if (freq == 1) // Shift the string LEFT one position
> {
> k = i + 1; // Point k to next entry
> j = i; // Point j to count
> do
> {
> str[j++] = str[k++];
> } while (str[k]);
> str[j] = '\0'; // Terminate the string
> }
> else // Shift
> the string to right and make room for expansion.
> {
> room=freq-2;
> len=strlen(str) ;
> for (j=len; j>i ;j--)
> {
> str[j+room]= str[j];
> }
> cout << endl << str << endl;
> j = i;
> curr_char = str[i-1];
> for (k = 0; k <= room; k++) //expand
> {
> str[j] = curr_char;
> j++;
> i++;
> }
> }
> cout << str << endl;
> }
> else
> { //error !!!
> cout << endl << str[i] << " is not a numeric value!" << endl;
> i++;
> err = 1;
> }
> }
> else
> { //error !!!
> cout << endl << str[i] << " is not an alpha character!" << endl;
> i++;
> i++;
> err = 1;
> }
> } // and done !!! :)
> printf ("\n%s\n",str) ;
>
> // system("PAUSE" );
> return EXIT_SUCCESS;
> }
>
> >----- Original Message ----
> >From: Thomas Hruska <[EMAIL PROTECTED] .com>
> >To: [EMAIL PROTECTED] com
> >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]
> >
> >
> >
> >To unsubscribe, send a blank message to
> ><mailto:c-prog-unsubscribe@ yahoogroups. com>.
> >Yahoo! Groups Links
> >
> >
> >
>
>
>
>
>
>       Download prohibited? No problem. CHAT from any browser, without
> download. Go to http://in.messenger.yahoo.com/webmessengerpromo.php/
>
> [Non-text portions of this message have been removed]
>
>
>
> To unsubscribe, send a blank message to <mailto:
> [EMAIL PROTECTED]>.
> Yahoo! Groups Links
>
>
>
>


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

Reply via email to