Suppose you have a char array (char name[6] = "Obama";) and this string has a length, which is 6 (remember the null terminated character = '\0'). The first character of the string is actually numbered 0, the second 1, and so on til th 5th character. So basically you have:
O = [0]
b = [1]
a = [2]
m = [3]
a = [4]
'\0' = [5]
What you have to do is put the character 'a' in the position [0].
I suggest you to create another empty char array of the same size, for example: char reversed_name[6];
In this new array (reversed_name) you have to put in the first position, which actually is the position [0], the last character of the other string (or array), which is actually at the position [4] (we don't count the '\0', because we don't want to reverse it of course, lololoolool), so you have this:
reversed_name[0] = name [4];
reversed_name[1] = name [3];
reversed_name[2] = name [3];
reversed_name[3] = name [2];
reversed_name[4] = name [1];
Now you have the name reversed, which is saved in reversed_name. You can do something like that:
1 2 3 4 5 6 7 8 9
|
char name[6] = "Obama";
char reversed_name[6];
for(int i=0, j=4; i<5 && j>=0; i++, j--)
reversed_name[i] = name[j];
reversed_name[5] = '\n';
std::cout << "The name is: "<<name<<"\n\n";
std::cout <<"The reversed name is: "<<reversed_name<<"\n\n";
|