Thread: rotating a string question..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    rotating a string question..

    i got a string and i want to rotate the odd members
    str "abcdef"
    1 rotate
    "afcbed"
    second rotate
    "adcfeb"

    etc..

    what is the algorithm for doing that..

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Which direction are you gonna rotate the string, left or right?
    How about you come with an algorithm and surely board members will help out.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    rotating to the right
    the index of "f" is 5 and it needs to be on 1
    the index of "d" is 3 and it needs to be on 5
    the index of "b" is 1 and it needs to be on 3
    so ill do copy each odd cell x to x+2 cell
    and the last will be copied to cell 1

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You pretty much solved it. So you can make a loop - something like
    Code:
    for (i = 1; i < 6; i += 2)
    ...

  5. #5
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    It seems like your "rotation" is more of randomization. Consider using srand for that (I'm not sure how effective this function is or what's called). Since you only want one instance of each letter, if the chosen one is already chosen, get another random number until you have something different. The "ace" part is unchanged so you only need to work with the other 3.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM
  5. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM