Like Tree1Likes
  • 1 Post By deadrabbit

Reverse contents of array

This is a discussion on Reverse contents of array within the C Programming forums, part of the General Programming Boards category; I found this algorithm to reverse the contents of an array- Code: int len=strlen(word); for (int i=0;i<len/2;i++) { word[i]^=word[len-i-1]; word[len-i-1]^=word[i]; ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    24

    Reverse contents of array

    I found this algorithm to reverse the contents of an array-
    Code:
    int len=strlen(word);
        for (int i=0;i<len/2;i++)
        {
            word[i]^=word[len-i-1];
            word[len-i-1]^=word[i];
            word[i]^=word[len-i-1];
        }
    But I really don't understand how it works. It looks as though the loop only traverses half of the array. If anyone could lend some insight I would appreciate it.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    What does the code in the loop body do?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    24
    It is supposed to reverse the contents of an array, that is take 4689 and turn it into 9864, using XOR operations.

    I found the algorithm here
    http://stackoverflow.com/questions/1...129028#1129028

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Make word an array of doubles, or pointers, or structs, and see how far it gets you.
    Question 3.3b
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Yes, but I am asking you specifically about the loop body, i.e.,
    Code:
    word[i]^=word[len-i-1];
    word[len-i-1]^=word[i];
    word[i]^=word[len-i-1];
    That said, now that I took a good look myself, I think you should examine this example instead:
    Code:
    int len = strlen(word);
    for (int i = 0; i < len / 2; i++)
    {
        char temp = word[i];
        word[i] = word[len - i - 1];
        word[len - i - 1] = temp;
    }
    The same question applies: what does the loop body do? Once you answer this correctly, you are on your way to answering your own question of why "the loop only traverses half of the array".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    24
    I figured it out; were swapping the first and last elements, then second and second to last element, etc. which means that we only need to go half way through the array to reverse the elements.
    Salem likes this.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Yes, that is correct
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Array To Function & Display Array Contents
    By mcertini in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 12:32 PM
  2. protecting array contents
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 01-22-2010, 09:23 AM
  3. comparing array contents
    By ReLiEnThAwK in forum C++ Programming
    Replies: 19
    Last Post: 08-09-2006, 07:52 AM
  4. Adding array contents
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:26 AM
  5. contents of array
    By Sway in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 06:26 PM

Tags for this Thread


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