Search Tutorials

Tuesday 23 April 2013

Java code to check whether a string is a Palindrome or not

Java program to check whether a string is a palindrome or not.

import java.io.*;
class Palindrome
{
static String n;
static int l;
 public static void main(String args[]) throws IOException
 {
 BufferedReader br = new BufferedReader(new
 InputStreamReader(System.in));
 // Read the String
 System.out.print("Enter a String : ");
 n = br.readLine();
 l = n.length();
 Palindrome();
 }
 public static void Palindrome()
 {
 int f = -1;
 for(int i=0;i<=l/2;i++)
 {
  if(n.charAt(i)!=n.charAt(l-1-i))
  {
   f=1;
   break;
  }
 }
 if(f==-1)
  System.out.println("\n" +n +" is a Palindrome");
 else
  System.out.println("\n" +n +" is NOT a Palindrome");
 }
}

ALGORITHM:-

1. Start

2. Accept a sentence from the user.

3. Extract each character from the sentence.

4. Reverse the String.

5. IF the string remains unchanged THEN "String is a Palindrome".
else
"String is NOT a Palindrome".

6. End.

OUTPUT:-

Java program for Palindrome


Enter a String : rotator

rotator is a Palindrome

Related Programs:- 

Find the sum of Natural numbers up to a given number using Recursion

Calculate the Power of a number using recursion

Print the Initials of a name

Find the frequency of each character in a string

Arrange the letters of a word in alphabetical order

No comments:

Post a Comment

Back to Top