Dark mode logo
Last Updated:
Palindrome of a string in Java

Palindrome of a String using Java

Algorithm

Input: Read a string from the user
Output: Displays if the string is Palindrome or not

1.Start
2.Read a string from the user
3.Find the length of the string and store it in length.
4.Convert the string to lower case 
5. While i < length / 2
  5.1 if ' i 'th character of string != (length-i-1) th character of the string
    5.1.1 found = 1
  5.2 if found == 1
    5.2.1 Print "The string is not Palindrome"
  5.3 else
    5.3.1 Print "The string is Palindrome"
6. Stop

Java Program

import java.util.Scanner;
public class Main {
        public static void main ( String[ ] args) {
            int check=0,i=0;
            Scanner sc = new Scanner(System.in);
           System.out.print("Enter a string: ");
            String str = sc.nextLine( );
           int len=str.length( );
           str=str.toLowerCase( );
           while (i<len/2){
                if (str.charAt(i)!=str.charAt(len-1-i) ) {
                    check = 1;
               }
                i++;
           }
           if (check ==1) {
                System.out.println("The String is not a Palindrome");
           } else {
                System.out.println("The String is a Palindrome");
           }
        }
    }

Output

Enter a string to check palindrome: Hello
The string is not a palindrome

Comments