Hello guys just trying to reverse a users input name here and its not working at all and i cannot understand it, Would appreciate some input thank you!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void reverse(string& name, int letters)
{
char hold;
int n = 0;
for(int i = letters - 1; i >= 0; i--)
{
hold = name[n];
name[n] = name[i];
name[i] = hold;
n++;
}
}
Letters int just represents how many letters are in the users name.
So i want it to just take the first character and replace it with the last and since both n and i go on to meet each other i dont see why it doesnt at least PARTIALLY work? I could understand if there is a bug when they get to the middle of the name but this works not at all!
That's because you swap each pair of characters twice. For example, the first letter is switched with the last one at the beginning of the loop and then again at the end of the loop.
I dont think i get it - N goes plus plus so shouldn't it then start replacing the second letter with the second to last letter on the second itteration of the loop?
At the beginning i = [14] and n = [0]
Lol sorry for the vague 14 - i am using my name: "Vlad_Lopatinsky" As the test string so thats where i get 14 from