Java program to find whether a string is palindrome or not
This post help you in finding whether a string is palindrome or not. Here we are copying the string from its last position to another string and checking whether they are equal or not. The time complexity is O(n). It can also be done without using additional memory.
import java.util.Scanner;
public class palindrome {
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
String original,reverse="";
do
{
System.out.println("Enter the string to check..");
original=in.nextLine();
reverse="";
int length=original.length();
for(int i=length-1;i>=0;i--)
{
reverse=reverse+original.charAt(i);
}
System.out.println("Original= "+original +" Reverse= "+reverse);
if(original.equalsIgnoreCase(reverse))
{
System.out.println("Palindrome");
}
else
{
System.out.println("Not Palindrome");
}
}while(!original.contains("quit"));
}
}
This comment has been removed by the author.
ReplyDelete