reverse function

This is a discussion on reverse function within the C Programming forums, part of the General Programming Boards category; could somebody explain the code below in simple english?errr how what happens and when? i tried drawing diagrams but i ...

  1. #1
    vatos
    Guest

    reverse function

    could somebody explain the code below in simple english?errr how what happens and when? i tried drawing diagrams but i just confused myself even more. my logic is failing me big time.

    thanx a lot

    Code:
    void rev( char * str ) {
       char temp, *end;
       end = str + strlen( str ) -1;
       
    
       while ( str < end ) { 
          temp  =  *s;
          *str++  =  *end;
          *end-- =  temp;
       }
    }

  2. #2
    vatos
    Guest

    rev again

    oops..there was a minor error in the previous code. here it is again!

    [code]

    void rev( char * str ) {
    char temp, *end;
    end = str + strlen( str ) -1;


    while ( str < end ) {
    temp = *str;
    *s++ = *end;
    *end-- = temp;
    }
    }
    [code]

  3. #3
    vatos
    Guest

    revv

    oh no...i did it again! sorry!

    Code:
    void rev( char * str ) {
       char temp, *end;
       end = str + strlen( str ) -1;
    
       while ( str < end ) {
          temp  =  *str;
          *str++  =  *end;
          *end-- =  temp;
       }
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: revv

    Comments added by me:

    Code:
    /* take a string 'str', return nothing */
    void rev( char * str ) {
        /*
            Declare 1 character variable, 'tem',
            and a pointer to a character 'end'.
        */
       char temp, *end;
    
        /*
            Point to the last character in the
            string. Pointer mathmatics. Add
            N-1 to the start of the pointer
            and you're at the end, where N
            is the length of the string.
        */
       end = str + strlen( str ) -1;
    
        /*
            while the pointer 'str' is less than
            the pointer 'end', do the following
        */
       while ( str < end ) {
            /*
                swap what is currently at each
                current pointer 'str' and 'end'
                and move each pointer closer
                to the other
            */
          temp  =  *str;
          *str++  =  *end;
          *end-- =  temp;
       }
    }
    Enjoy.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    vatos
    Guest

    Re: Re: revv


    /*while the pointer 'str' is less than
    the pointer 'end', do the following
    */
    while ( str < end )
    do u mean less in value? please explain. thank you so much.

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    What value? it's a pointer, it holds an memory adress, while the adress of str is smaller than end adress, do the follow.

  7. #7
    vatos
    Guest
    ok i get it now! thanx!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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