We can check palindrome string by reversing string and checking whether it is equal to the original string or not.
public class Palindrome {
public static void main(String[] args) {
String str = "OOLOO";
StringBuffer newStr =new StringBuffer();
for(int i = str.length()-1; i >= 0 ; i--) {
newStr = newStr.append(str.charAt(i));
}
if(str.equalsIgnoreCase(newStr.toString())) {
System.out.println("String is palindrome");
} else {
System.out.println("String is not palindrome");
}
}
}
checkIfPalindrome(String s)
{
StringBuilder s2 = new StringBuilder(s);
s2.reverse();
String rev_s2 = s2.toString();
if(s.equals(rev_s2))
{ return true; }
else
{ return false; }
}
(OR)
{
StringBuilder s2 = new StringBuilder(s);
s2.reverse();
String rev_s2 = s2.toString();
if(s.equals(rev_s2))
{ return true; }
else
{ return false; }
}
(OR)
public class Palindrome {
public static void main(String[] args) {
String str = "OOLOO";
StringBuffer newStr =new StringBuffer();
for(int i = str.length()-1; i >= 0 ; i--) {
newStr = newStr.append(str.charAt(i));
}
if(str.equalsIgnoreCase(newStr.toString())) {
System.out.println("String is palindrome");
} else {
System.out.println("String is not palindrome");
}
}
}
No comments:
Post a Comment