Thread: shifting elements in array

  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
    28,412
    You could use std::rotate from <algorithm>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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,318
    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
    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, 05: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, 12:11 PM
  5. shifting array elements
    By dP munky in forum C Programming
    Replies: 5
    Last Post: 04-17-2003, 01:35 PM