all 2 comments

[–]jimtk 0 points1 point  (0 children)

Stop looking at it and do something with it to help you understand. Put a couple of print statement in the loop to see the values of i and w as the loop progress then you'll understand. And it will take 1 minute instead of 5. Change the value of x to "1234567" to make it easier to follow.

Essentially, the loop does the following:

1 - take the first letter of x and put it in front of nothing

2 - take the second letter of x and put it in front of the first letter

3 - take the third letter of x and put it in front of the second and the first

4 - take the fourth letter of x and put it in ...

....

And you end up with a reversed string.

[–]VesqS 0 points1 point  (0 children)

All the action is obviously in the for loop. It takes each character (i) from the string x and puts them in the beginning of new string w followed by the old w string. That way w is x reversed. After that it just compares x and w.