Hi all,
I am trying to write a code to find out the palindrome words from a sentence. Palindrome words are those words which remains same while reading from left to write or right to left like LEVEL, MADAM, MOM, DAD.
My programming is giving output but with this exception
"Exception in thread "main" java.lang.StringIndexOutOfBoundException: String index out of range: 43"
Kindly help me to come out of this issue.... I don't want to use "Split" function.
Here is my code:
public class Sentence { public static void main(String [] args) { String str = "HELLO ROTATOR WHERE MADAM IS LEVEL AND POP."; int i=0; int j=0; int s=0; String word; String temp; String str2; int len=str.length(); for(i=s;i<str.length()-1;) { str2=""; while((str.charAt(j)!=' ') && (str.charAt(j)!='.')) { str2=str2+str.charAt(j); j++; } s=++j; word = reverse(str2); word=word.intern(); str2=str2.intern(); if(str2==word) { System.out.println(str2 +"is palindrome"); } } } public static String reverse(String temp) { String str3 = ""; for(int k=temp.length()-1;k>=0;k--) { str3= str3+temp.charAt(k); } return str3; } }