Check String given as command line argument is Palindrome or not?
import java.util.*;
class Palindrome
{
public static void main(String args[])
{
String originalString, reverseString="";
originalString = args[0];
int length = originalString.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverseString = reverseString + originalString.charAt(i);
if (originalString.equals(reverseString))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
}
}
class Palindrome
{
public static void main(String args[])
{
String originalString, reverseString="";
originalString = args[0];
int length = originalString.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverseString = reverseString + originalString.charAt(i);
if (originalString.equals(reverseString))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
}
}
- Save the file with name Palindrome.java
- Compile with: javac Palindrome.java
- Run with: java Palindrome malayalam
- Run with: java Palindrome madam
- Run with: java Palindrome Java
- Run with: java Palindrome good
easy program to understand
ReplyDelete