Issue
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Index 6 out of bounds for length 6
Process finished with exit code 1
public class Main {
boolean palindrome(String str, int start, int end){
if(start >= end)
return true;
return(str.charAt(start) == str.charAt(end)) && palindrome(str, start+1, end-1);
}
public static void main(String[] args) {
String str = "aabbaa";
int n = str.length();
System.out.println(palindrome(str, 0, n));
}
}
Solution
You should use int n = str.length() - 1;
which is how you get the last index.
if str.length()
is 6, the indexes are 0-5 not 1-6.
Answered By - Roee Gavirel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.