Thread: 2 versiion of reverse string

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    2 versiion of reverse string

    Hello I am trung following two versions of string reversion.

    But I am getting unhandles exception at red lines...

    Please advise..



    Code:
    # include<stdio.h>
    
    
    /*void reverse(char *p)
    {
    	static char firstTime = 1;
    	static char *s;
    	
    	if (firstTime)
    	{
    		s = p;
    		firstTime = 0;
    	}
    
    	if ( *p == '\0')
    		return;
    	
    	reverse(p+1);
    	*s++ = *p;
    }*/
    
    my_reverse(char *a,char *b,int len)
    {
    	while( len-- )
    	{
    		(*a) = (*a) ^ (*b);
    		(*b) = (*b) ^ (*a);
    		(*a) = (*a) ^ (*b);
    
    		a++;
    		b--;
    	}
    }
    
    
    main()
    {
    //	reverse("I_AM_FINE");
    	char *p = "I_AM_FINE";
    
    	printf("%s\n",p);
    	my_reverse(p,p+strlen(p)-1,strlen(p)/2);
    	printf("%s\n",p);
    
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    char *p = "I_AM_FINE";
    String literals should be considered read-only. Perhaps you meant to use an initialized char array.
    Code:
    char p[] = "I_AM_FINE";
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Hello All,

    It works

    if I define p as

    Code:
    char a[] = {'I','_','A','M','_','F','I','N','E','\0'};
    char *p = a;
    
    instead of 
    
    char *p = "I_AM_FINE";
    But recusrsive has some problems

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    String literals should be considered read-only.

    Dave: Can you please explain this or give some pointers?

    Thanks,

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    When you have "Hello I'm a string literal" many (dare I say most) compilers will have this placed into a read-only portion of memory. This is important because you don't want your literals changing between function calls. So when you do:
    Code:
    char *p = "Hello I'm a string literal";
    you are pointing to the location in memory in which the literal resides. Trying to change the values at that location (remember its read-only) can/will result in a seg fault since you don't have access to change it's value.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Can you please explain this or give some pointers?

    The standard lists as undefined behavior when "the program attempts to modify a string literal". Some systems choose to put a string literal somewhere that cannot be written to (attempting do so so might generate an unhandled exception, for example).

    [edit]Damn! Damn these pokey fingers!
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    (*a) = (*a) ^ (*b);
    (*b) = (*b) ^ (*a);
    (*a) = (*a) ^ (*b);
    This is a horrible way to swap two values.

    It's also wrong - consider what happens when a and b both point at the same char - like when they meet in the middle for instance.
    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.

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    PHP Code:
    It's also wrong - consider what happens when a and b both point at the same char - like when they meet in the middle for instance. 
    Give example.

    5 ^5 = 0
    0 ^5 = 5
    5 ^0 = 5

    Still works.

    Please let me know...

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Give example.
    Code:
    #include <stdio.h>
    
    void badswap(int *a, int *b)
    {
       (*a) = (*a) ^ (*b);
       (*b) = (*b) ^ (*a);
       (*a) = (*a) ^ (*b);
    }
    
    int main()
    {
       char x = 42;
       printf("x = %d\n", x);
       badswap(&x, &x);
       printf("x = %d\n", x);
       return 0;
    }
    
    /* my output
    x = 42
    x = 0
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Why XOR truth table does not work here.

    I tried something like this.

    First printf in badswap prints garbage..

    Code:
    #include <stdio.h>
    
    void badswap(int *a, int *b)
    {
       printf("a = %d b = %d\n",*a,*b);
       (*a) = (*a) ^ (*b);
       printf("a = %d b = %d\n",*a,*b);
       (*b) = (*b) ^ (*a);
       printf("a = %d b = %d\n",*a,*b);
       (*a) = (*a) ^ (*b);
       printf("a = %d b = %d\n",*a,*b);
    }
    
    int main()
    {
       char x = 42;
       printf("x = %d\n", x);
       badswap(&x, &x);
       printf("x = %d\n", x);
       return 0;
    }

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Recusive way of swapping string

    Code:
    void my_reverse(char *p)
    {
    	static char firstTime = 1;
    	static char *s = NULL;
    	char temp;
    	
    	if (firstTime)
    	{
    		s = p;
    		firstTime = !firstTime; // = 0;
    	}
    
    	if ( *p == '\0')
    		return;
    	temp = *p;
    	my_reverse(p+1);
    	*s++ = temp;
    }

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    M'kay.
    Code:
    #include <stdio.h>
    
    void badswap(char *a, char *b)
    {
       (*a) = (*a) ^ (*b);
       (*b) = (*b) ^ (*a);
       (*a) = (*a) ^ (*b);
    }
    
    int main()
    {
       char *p, *q, text[] = "hello world";
       puts(text);
       for ( p = text, q = &text[sizeof text - 2]; p <= q; ++p, --q )
       {
          badswap(p,q);
       }
       puts(text);
       return 0;
    }
    
    /* my output
    hello world
    dlrow
    */
    See how swapping an object with itself can do unwanted things?

    Now there is no need to swap a value with itself, so the test could be written p < q. But the point was that this XOR trick is an old hack that has more drawbacks and few advantages compared with the simple straightforward swap.
    Code:
    #include <stdio.h>
    
    void goodswap(char *a, char *b)
    {
       char temp = *a;
       *a = *b;
       *b = temp;
    }
    
    int main()
    {
       char *p, *q, text[] = "hello world";
       puts(text);
       for ( p = text, q = &text[sizeof text - 2]; p <= q; ++p, --q )
       {
          goodswap(p,q);
       }
       puts(text);
       return 0;
    }
    
    /* my output
    hello world
    dlrow olleh
    */
    And for me, goodswap generates less code than badswap.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    Quote Originally Posted by Roaring_Tiger
    Recusive way of swapping string

    Code:
    void my_reverse(char *p)
    {
    	static char firstTime = 1;
    	static char *s = NULL;
    	char temp;
    	
    	if (firstTime)
    	{
    		s = p;
    		firstTime = !firstTime; // = 0;
    	}
    
    	if ( *p == '\0')
    		return;
    	temp = *p;
    	my_reverse(p+1);
    	*s++ = temp;
    }
    Not good, not good at all. You never set firstTime variable back to 1. And IMO some crappy flag var in the loop is bad (of course this is _VERY_ bad use of recursion). Anyhow, try this:
    Code:
    void my_reverse(char *pStr)
    {
    	static char *pStrT;
    	static int n = 0;
    	char cTemp;
    	
    	if (*pStr == '\0')
    	{
    		pStrT = pStr-n;
    		n = 0;
    		return;
    	}
    
    	n++;
    	cTemp = *pStr;
    	my_reverse(pStr+1);
    	*pStrT++ = cTemp;
    }

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why XOR truth table does not work here.
    XOR is basically one or the other, but not both. If both bits in the test are set then the result is a clear bit. Therefore, XORing a variable with itself results in 0. This is rarely what you want when swapping the contents of two pointers that reference the same variable.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM