Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

String Palindrome in Java

Author: Akshay Sharma
by Akshay Sharma
Posted: May 21, 2022

A palindrome string is a string that is the same after reverse. For example, radar, level, etc.

In this program, we will learn to check whether a string is a palindrome in Java.

To understand this example, you should know about the basic Java programming topics like strings, loops, conditional statements, etc. So please refer to those topics before jumping into the string palindrome in java problem.

Problem Statement

Given a string, write a java program to check if it is palindrome or not.

Example

Input:

Radar

Output:

Radar is a palindromic string.

Explanation:

If we read the string from end to beginning, it is the same as beginning to end.

Input:

Java

Output:

Java is not a palindromic string..

Explanation:

If we read the string from end to beginning, it is not the same as beginning to end.

Let us now discuss the approaches for the string palindrome in Java problem.

Approach: 1

The first approach for the string palindrome in Java problem is as follows.

  • The string is stored in str.

  • The for loop runs from the end to the beginning of the given string.

  • The charAt() method accesses each character of the given string.

  • Each character of the given string is accessed in reverse order and stored in reverseStr variable.

  • The toLowerCase() method converts both the string and the reversed string to lowercase. This is because Java is case sensitive language and 'r' and 'R' are two different values.

  • The equals() method is used to check if two strings, str and reverseStr are equal.

  • If the string str and reverseStr are equal, the string is a palindromic string.

  • If the string str and reverseStr are not equal, the string is a not palindromic string.

Example for a Palindromic String

Let us take the string as "Level".

Code

// Program to check string palindrome in Java

class Main {

public static void main(String[] args) {

// Given string

String str = "Level";

// Reversed String

String reverseStr = "";

// Length of the string

int strLength = str.length();

// Reverse the string using a for loop

for (int i = (strLength - 1); i>=0; --i) {

reverseStr = reverseStr + str.charAt(i);

}

// If the string and reversed string is equal, it is a palindrome

if (str.toLowerCase().equals(reverseStr.toLowerCase())) {

System.out.println(str + " is a palindromic string.");

} else {

// If the string and reversed string is not equal, it is not a palindrome

System.out.println(str + " is not a palindromic string.");

}

}

}

Output

Level is a palindromic string.

Example for a non - Palindromic String

Let us take the string as "proGram".

Code

// Program to check string palindrome in Java

class Main {

public static void main(String[] args) {

// Given string

String str = "proGram";

// Reversed String

String reverseStr = "";

// Length of the string

int strLength = str.length();

// Reverse the string using a for loop

for (int i = (strLength - 1); i>=0; --i) {

reverseStr = reverseStr + str.charAt(i);

}

// If the string and reversed string is equal, it is a palindrome

if (str.toLowerCase().equals(reverseStr.toLowerCase())) {

System.out.println(str + " is a palindromic string.");

} else {

// If the string and reversed string is not equal, it is not a palindrome

System.out.println(str + " is not a palindromic string.");

}

}

}

Output

proGram is not a palindromic string.

Time Complexity

O(n), where n is the length of the string.

As the for loop traverses from 1 to n.

Space Complexity

O(n), where n is the length of the string.

As a new string of length, "n" is created.

Approach: 2

The second approach for the string palindrome in Java problem is as follows.

  • The string is stored in str.

  • First, convert the string to lowercase.

  • Take two pointers, "i" pointing to the start of the string and "j" pointing to the end of the string.

  • Keep incrementing the "i" pointer and decrementing "j" pointer while i

    About the Author

    Akshay is a tech expert and dealing with different programming languages i.e Java, Python, Data Science, & Sql.

    Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
Author: Akshay Sharma

Akshay Sharma

Member since: May 18, 2022
Published articles: 5

Related Articles