String palindrome program in Java

Checking string palindrome in Java: Here, we are going to learn how to check whether a given string is palindrome string or not?
Submitted by IncludeHelp, on July 12, 2019

Given a string and we have to check whether it is palindrome string or not.

A string that is equal to its reverse string is known as palindrome string. To implement the program for checking whether a given string is a palindrome or not, we have created a function "isPalindrome()".

In the function,

  • We are checking for whether a string is an empty string or not – if the string is an empty string then throwing an error.
  • Then, we are converting string to uppercase to make comparison case insensitive.
  • Then, running a loop from 0 to len/2, to compare the first character with last character, the second character with second last character and so on..., and checks whether they are equal or not if both the elements are equal it goes for the next one. If not, then code returns false. Going on comparing first and last elements of the string if it reaches the length/2 mark then the loop ends, and return true for Palindrome.

Java code for checking string palindrome

// Java code for checking string palindrome 

public class Main {
    //function to check whether string is Palindrome or not
    public static boolean isPalindrome(String str) {
        // Checking for null
        if (str == null) {
            throw new IllegalArgumentException("String is null.");
        }

        // length of the string
        // if there is one character string - returing true
        int len = str.length();
        if (len <= 1) {
            return true;
        }

        // Converting the string into uppercase 
        // to make the comparisons case insensitive 
        String strU = str.toUpperCase();

        // result variable
        // default initializing it with true
        boolean result = true;

        for (int i = 0; i < len / 2; i++) {
            if (strU.charAt(i) != strU.charAt(len - 1 - i)) {
                result = false;
                // break the loop if the condition is true
                break;
            }
        }
        return result;
    }

    //main code  
    public static void main(String[] args) {
        String str1 = "Hello world!";
        if (isPalindrome(str1)) {
            System.out.println(str1 + " is a palindrome string ");
        } else {
            System.out.println(str1 + " is not a palindrome string ");
        }

        String str2 = "ABCxCBA";
        if (isPalindrome(str2)) {
            System.out.println(str2 + " is a palindrome string ");
        } else {
            System.out.println(str2 + " is not a palindrome string ");
        }

        String str3 = "noon";
        if (isPalindrome(str3)) {
            System.out.println(str3 + " is a palindrome string ");
        } else {
            System.out.println(str3 + " is not a palindrome string ");
        }

        String str4 = "nooN";
        if (isPalindrome(str4)) {
            System.out.println(str4 + " is a palindrome string ");
        } else {
            System.out.println(str4 + " is not a palindrome string ");
        }
    }
}

Output

Hello world! is not a palindrome string
ABCxCBA is a palindrome string
noon is a palindrome string
nooN is a palindrome string

Java String Programs »



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.