reverse

This is a discussion on reverse within the C++ Programming forums, part of the General Programming Boards category; I found this code.. It works just fine. but I really dont know how can it reverse the string? shouldnt ...

  1. #1
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117

    reverse

    I found this code..
    It works just fine.
    but I really dont know how can it reverse the string?
    shouldnt it end at the middle of the string? (target < e)
    and what char tmp does?

    Code:
    void rev(char* target)
    {
          for(char* e = target+length(target)-1; target < e; ++target, --e)  //length is the same as strlen.
          {
    	  char tmp = *target;
    	  *target = *e;
    	  *e = tmp;
          }
    }
    Luigi

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Basically, what this code does is it swaps the beginning and the end bytes, and keeps moving inward until it reaches the middle. By then, all the bytes have been swapped with a byte the same distance from the end, and the string has been reversed. That's why it's supposed to end at the middle.

    char tmp acts as a temporary variable. Often, when programmers swap two variables, a and b, they do this:
    Code:
    int tmp;
    
    tmp = a;
    a = b;
    b = tmp;
    Or something along those lines. You can't do this kind of swap (safely) without a temporary variable.

  3. #3
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    the way I see it..
    lets say I input "abcd"

    target will end up like this : "dccd"
    and e this way : "abba"

    Code:
              char tmp = *target;
              *target = *e;   //target = end of target
              *e = tmp;   // e = begining of target
                                // but e is not target?
                                // so why does it work anyway?
    Luigi

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Very similiar to my own strreverse:

    Code:
    __inline void strreverse (char* str)
    {
    	char *last = str + (strlen(str) - 1);
    	for (char *p = last; str < p; ++str, --p)
    	{
    		char t = *str;
    		*str = *p;
    		*p = t;
    	}
    }
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  5. #5
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Luigi:

    for abcd,

    it first swaps the first and the last, becoming

    dbca

    then moves inward 1, and swaps the second from the first and the second from the last, becoming

    dcba

    Which is the string reversed.
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  6. #6
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    ho I think I get it.
    when u do this:

    *e = tmp;

    you do change target since e is a pointer to target.
    thats why, right?

    Luigi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my reverse function and its output!
    By Matus in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 08:33 PM
  2. a reverse function
    By AngKar in forum C Programming
    Replies: 20
    Last Post: 04-27-2006, 10:35 PM
  3. Using reverse iterators in algorithms
    By 0rion in forum C++ Programming
    Replies: 1
    Last Post: 02-27-2006, 02:19 AM
  4. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM
  5. Replies: 7
    Last Post: 03-18-2003, 02:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21