Java Code - String Reverser

Three years ago, I remembered my teacher teaching us how to create a program that reverses a string. But before that, I tried to solve the problem, which I did. :D

The code below reverses a string:

private static String reverse( String in )
{
char tmp;
char[] arr = in.toCharArray();
int i, y;
for( i = in.length()-1, y = 0; y < arr.length / 2; i--,y++ ){
tmp = arr[i];
arr[i] = arr[y];
arr[y] = tmp;
}

return new String(arr);
}

It is easy to understand :-)

Note: The code is in JAVA but you can do the same to other languages following the algorithm.

Leave a Reply