I am trying to write code in java to reverse string enter by user the program runs fine without any errors but as I press enter after entering the string it show the error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12 at javaprograms.JavaPrograms.main(JavaPrograms.java:16) C:\Users\hp\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 8 seconds)"
Here is My code:
package javaprograms;
import java.util.Scanner;
public class JavaPrograms {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String str;
int length;
System.out.println("Enter String: ");
str=s.nextLine();
length=str.length();
char[] arr;
arr=str.toCharArray();
for(int i=length ; i>0 ; i--)
{
System.out.print(arr[i]);
}
}
}