Thread: Rotate char array

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    4

    Rotate char array

    Hello,

    I want to rotate an array, but I haven't had any luck yet. I want it like this:

    Code:
    char array[3][4] = {'0'};
    
    array[0] = "haai";
    array[1] = "hiii";
    array[2] = "hooh";
    I want it to rotate by one, to the right, like:

    array[0] = array[1]
    array[1] = array[2]
    array[2] = array[0]

    so instead of "haaihiiihooh" you have "hiiiihoohhaai". The index of the array is different all the time, depending on the user input.

    Can someone help me with this one please?

    Thanks.

  2. #2
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    does that even compile?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    Who cares, I just need the strings in the char array rotated.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    One of the easiest ways would be to use a vector or other STL container of string objects along with the rotate function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Is this what you are looking for?
    Code:
    char *  arr[3] = {"haai", "hiii", "hooh"};
    char * temp;
    
    temp = arr[2];
    arr[2] = arr[1];
    arr[1] = arr[0];
    arr[0] = temp;
    There's a number of different ways to go at your problem... could use more detail.
    Callou collei we'll code the way
    Of prime numbers and pings!

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    19
    Code:
    char array[3][4] = {'0'};
    
    array[0] = "haai";
    Can you hold a string in a char? :P

    Also, your array is multi-dimentional yet you are only pointing to one dimention.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    Thanks all, I got it working.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    Hello again,

    I use this code to rotate the array, but I need some code to rotate it back.

    Code:
     
    string temp = "";
    temp = cBlok[iBlokAantal - 1];
    
    for (int i = (iBlokAantal - 1); i > -1; i--)
    {
    	if (i == 0)
    	{
    		for(int x = 0; x < 16; x++)
    		{
    			cBlok[i][x] = temp[x];
    		}
    	}
    	else
    	{
    		for(int x = 0; x < 16; x++)
    	        {
                              cBlok[i][x] = cBlok[i-1][x];
    		}
    	}
    }
    Can someone help me to rotate it back?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Do you understand what that code is doing? To rotate back you can do the same, but loop in the other direction and use +1 instead of -1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM