Write a java program that checks whether a given string is palindrome or not .

 

Write a java program that checks whether a given string is palindrome or not. Example:madam is a palindrome.


Program:


import java.io.*;
import java.util.*;
public class palindrome
{
public static void main(String args[ ])
throws IOException
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter the string to check for palindrome:");
String s1=sc.next();
StringBuffer sb=new StringBuffer();
// Or StringBuilder sb=new StringBuilder();
sb.append(s1);
sb.reverse();
String s2=sb.toString();
if(s1.equals(s2))
System.out.println("String is palindrome");
else
System.out.println("String is not palindrome");
}
}


Or


Program:-

public class palindrome
{
public static void main(String args[])
{
   String s="",s1="";
if(args.length==0)

System.out.println("no arguments");
}
else
{
s=args[0];
int l,i;
l=s.length();
for(i=l-1;i>=0;i--)
{
s1=s1+s.charAt(i);
}
if(s.equals(s1)){
System.out.println("String "+s+" is palindrome");}
else
System.out.println("String "+s+" is not palindrome");
}
}
}

Comments

Popular posts from this blog

Control Statements:Selection statement ,Iteration statement and Jump statement in Java

Abstract classes and Abstract methods in Java with Examples rcub.

Applets - Inheritance hierarchy for applets, differences between applets and applications, life cycle of an applet, passing parameters to applets, applet security issues.