Monday, January 10, 2011

BlueJ Program On String Palindrome Checking

what is string palindrome?



 A word or sentence is said to be palindrome if it is same while read from the left to right and right to left.  One example of palindrome word is ‘madam’.

 How to proceed for this string palindrome checking program

Firstly user will enter a text value and it will be stored in a string object. Another string object should be in the program to store the reversed value of the entered text. At the end we will get two string objects and equality checking on these string objects will confirm whether the string is palindrome or not.

Here is the program

import java.io.*;
class Palindrome
{
 String str1, str2;
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 public void takeString ()throws Exception
 {
  System.out.println("Enter a sentence:");
  str1=br.readLine().trim();
  }
   public void showResult ()
   {
    int i,len;
    str2=" ";
    len=str1.length ();
    for(i=len-1;i >=0;i--)
    {
    str2=str2+str1.charAt(i);
    }
    str2=str2.trim();
    if(str1.equals(str2))
    System.out.println(str1 +" is a palindrome string");
    else
    System.out.println(str1 +" is not a palindrome string");
   }
   public static void main (String args[])throws Exception
   {
   Palindrome obj=new Palindrome ();
   obj.takeString ();
   obj.showResult ();
  }
 }

 Modification of the above string palindrome checking program

Our program is to check whether a string, it may be a word or sentence is a palindrome. In the above program, we have reversed the string and stored the reversed value in another string object. During reversing the string, the loop executed its full course of iteration. Actually the program is on string palindrome checking, not to reverse the string but we have reversed the string to check the equality.  All the user entered strings would not be palindrome but our above program will reverse the string and will check the original string with its reversed version to reach the conclusion.

We can modify the above program so that it will reduce time complexity of the program. Take the string and execute a loop depending on the length of the string. Within the loop body our program will check characters from both end of the string, 1st character with the last character, 2nd character with the last but one character and so on for equality. If mismatch is found in the checking, the loop will be terminated to display that the string is not palindrome. If it happens that no mismatch is found during the entire checking process, the string will be palindrome.

Here is the modified program

import java.io.*;
class Palindrome
{
 String str1;
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 public void takeString ()throws Exception
 {
  System.out.println("Enter a sentence:");
  str1=br.readLine().trim();
  }
   public void showResult ()
   {
    int i,j=0,len;
    len=str1.length ();
    for(i=len-1;i >=0;i--)
    {
    if(str1.charAt(i)!=str1.charAt(j))
    break;
    j++;
    }
    if(i< 0)
    System.out.println(str1 +" is a palindrome string");
    else
    System.out.println(str1 +" is not a palindrome string");
   }
   public static void main (String args[])throws Exception
   {
   Palindrome obj=new Palindrome ();
   obj.takeString ();
   obj.showResult ();
  }
 }

 Technical analysis of the palindrome checking program

Taking the string from user, calculating the length of string etc is same as previous program. One point to keep in mind that while taking any string from user always use trim () function to eliminate unnecessary leading or trailing spaces from the string object.

Modifications are in side the loop body. Two variables are used for palindrome checking. Initially loop control variable points the last character of the string and another variable ‘j’ points the 1st character of the string. On each iteration, two characters of opposite direction of the string are checked for equality and loop control variable moves towards end - the other variable moves towards the beginning. If any difference is found, the loop terminates.

Outside the loop body it is examined whether the loop terminates after full running or it’s a premature termination and accordingly result is displayed.

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner