Like Tree2Likes
  • 1 Post By laserlight
  • 1 Post By iMalc

shifting elements in array

This is a discussion on shifting elements in array within the C++ Programming forums, part of the General Programming Boards category; what is the easiest way to shift array "abcdefg" to "defgabc"?...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    22

    shifting elements in array

    what is the easiest way to shift array "abcdefg" to "defgabc"?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    You could use std::rotate from <algorithm>.
    King Mir likes this.
    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
    Nov 2012
    Posts
    22
    is there something else than that? like directly shifting letters from one end to the other?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    Quote Originally Posted by gamers18
    like directly shifting letters from one end to the other?
    Sure, if you want you can implement the rotation manually.
    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

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Yes you can do a shift on the elements with your own function.

    In general you have to have an array big enough to handle the shifting. In the example we want to end up with something like:
    abcdefg
    bcdefga
    cdefgab
    defgabc

    The easiest way to do that is to have space in reserve at the end of the array... this will help you when you actually copy the characters in the front of the pivot ('d') to make space for the characters behind the pivot ('d') and place them in their new positions at the end of the array. The shifting part comes in when you have to scoot the pivot character and all the characters behind it into their new places.

    You don't have to move one character at a time either. Using a group of three might be easier to program, but using one character at a time was easiest for me to explain what you need to do. That should give you plenty to think about. HTH.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Or there's another easy way, with no extra space required:
    Reverse each "half" of the string, where by half, I mean split at the point of rotation.
    Reverse the whole thing.
    I.e
    abcdefg -> cbagfed -> defgabc
    Elkvis likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having a hard time shifting elements
    By saldar05 in forum C Programming
    Replies: 8
    Last Post: 12-03-2012, 04:43 AM
  2. Deleting and shifting elements in an array
    By sia927777 in forum C Programming
    Replies: 3
    Last Post: 10-13-2010, 01:34 AM
  3. Array Shifting C++
    By Flaug in forum C++ Programming
    Replies: 24
    Last Post: 04-13-2009, 09:40 AM
  4. Shifting elements in an Array
    By mmarab in forum C Programming
    Replies: 5
    Last Post: 12-10-2007, 11:11 AM
  5. shifting array elements
    By dP munky in forum C Programming
    Replies: 5
    Last Post: 04-17-2003, 01:35 PM

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