-
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.
-
-
Who cares, I just need the strings in the char array rotated.
-
One of the easiest ways would be to use a vector or other STL container of string objects along with the rotate function.
-
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.
-
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.
-
Thanks all, I got it working.
-
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?
-
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.