writing a recursive string reverse function out of curiosity, but having a bit of problem with XOR there. The whole point of this function, is to not use iterator, which is why it is a recursive function. this is not homework, just curiosity.
private static char[] ReverseNL(char[] arr, int index)
{
var len = arr.Length;
if (index > 0)
arr[len - index] ^= arr[index - 1];
return index-- < 1 ? arr : ReverseNL(arr, index);
}
it seems to jamble the first part of my string
"hey there stack!" becomes "I♫→A ←E↨reht yeh"
it is always the first half of the phrase that gets jumbled...
UPDATE..
i suppose XOR wasn't really needed here.. so used basic assignment, also got rid of return.
private static void ReverseNL(char[] arr, int index) {
var len = arr.Length;
if (index > 0 && index > len / 2) {
var c = arr[len - index];
arr[len - index] = arr[index - 1];
arr[index - 1] = c;
index--;
ReverseNL(arr, index);
}
}