How can this code be reversed? (recursion)

Obviously, it’s the recursion that confuse you. So, here’s an example:

string word = "world";
  • First recursion will split "world" to: "w" , "orl" and "d" and it will pass "orl" to the second recursion.
  • Second recursion will split "orl" to: "o" , "r" and "l" and it will pass "r" to the third recursion.
  • Third recursion will do nothing since size of "r" <= 1. And, now you go back to second recursion
  • Second recursion now will swap "o" and "l" and leave "r" as it is, which means: "lro" and this goes back to the first recursion.
  • Finally first recursion will swap "w" and "d" and leave "lro" as it is, which means: "dlrow". Which is the reversed of "world"

Leave a Comment