Java Program to determine whether a given string is palindrome

In this program, we need to check whether a given string is palindrome or not.

A string is said to be palindrome if it is the same from both the ends. For e.g. above string is a palindrome because if we try to read it from backward, it is same as forward. One of the approach to check this is iterate through the string till middle of string and compare a character from back and forth.

ALGORITHM

  • STEP 1: START
  • STEP 2: DEFINE String string = “Kayak”
  • STEP 3: SET flag = true
  • STEP 4: CONVERT string into lowercase.
  • STEP 5: SET i =0. REPEAT STEP 6 to STEP 7 UNTIL i
  • STEP 6: IF (string.charAt(i) != string.charAt(string.length()-i-1))
                  then
                  SET flag = false
                  break
  • STEP 7: SET i = i + 1
  • STEP 8: IF flag
                  then PRINT “Yes”
                  else
                  PRINT “No”
  • STEP 9: END

Program:

  1. public class PalindromeString    
  2. {    
  3.     public static void main(String[] args) {    
  4.         String string = “Kayak”;    
  5.         boolean flag = true;    
  6.             
  7.         //Converts the given string into lowercase    
  8.         string = string.toLowerCase();    
  9.             
  10.         //Iterate the string forward and backward, compare one character at a time     
  11.         //till middle of the string is reached    
  12.         for(int i = 0; i < string.length()/2; i++){    
  13.             if(string.charAt(i) != string.charAt(string.length()-i-1)){    
  14.                 flag = false;    
  15.                 break;    
  16.             }    
  17.         }    
  18.         if(flag)    
  19.             System.out.println(“Given string is palindrome”);    
  20.         else    
  21.             System.out.println(“Given string is not a palindrome”);    
  22.     }    
  23. }    

Output:

Given string is palindrome 

Leave a Comment