Reverse a String
Write a program to take a string as input and output its reverse.
The given code takes a string as input and converts it into a char array, which contains letters of the string as its elements.
Sample Input:
hello there
Sample Output:
ereht olleh
Code:
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String Str = in.nextLine();
char temp;
char[] arr = Str.toCharArray();
int len = arr.length;
for(int i=0; i<(Str.length())/2; i++,len--){
temp = arr[i];
arr[i] = arr[len-1];
arr[len-1] = temp;
}
System.out.println(String.valueOf(arr));
}
}