DEV Community

Akshay Sharma
Akshay Sharma

Posted on

How to check Palindrome String in java?

Introduction

In this article, we will find string palindromes in Java, it works the same as in integer. The meaning of the palindrome string is that if we start reading it from right to left or left to right it is the same in both cases.

Algorithm

Approach 1

For checking string palindromes in java, we can choose the characters one by one from the start and end of the string and compare each other.

=> Choose the first and the last character of the string and compare, if both the characters match then continue, else the string is not a palindrome.
=> Choose the second character from the first and last of the string and compare, if both match - continue, else return string is not a palindrome.
=> Continue the above comparisons till both the characters to compare are the same or adjacent to each other.

Approach 2

Instead of comparing the characters from start to end, we can find the reverse string of the given string and compare both the strings, if they are the same that means the string is a palindrome.

=> From given string, get character array
=> Create a string by iterating the array from the end to the start index.
=> Remove separator, comma, or other dividers from both the strings (Optional)
=> Finally, compare both the strings

Example

1. String Palindrome in java using Loop

import java.util.Scanner;

public class Main 
{
  public static void main(String[] args) 
  {
    System.out.print("Enter any string : ");
    Scanner in = new Scanner(System.in);
    String origString = in.nextLine();
    String reverseString = "";

    char[] characters = origString.toCharArray();

    for( int i = characters.length - 1 ; i >= 0 ; i-- ) {
      reverseString = reverseString + characters[i];
    }

    //Check palindrome string
    if (origString.equals(reverseString)) {
      System.out.println("String is a palindrome.");
    } else {
      System.out.println("String is not a palindrome.");
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Output:
Enter any String: Weather
String is not a palindrome.

Enter any String: Naman
String is a palindrome.

Example

class Main {
  public static void main(String[] args) {

    String str = "Radar", reverseStr = "";

    int strLength = str.length();

    for (int i = (strLength - 1); i >=0; --i) {
      reverseStr = reverseStr + str.charAt(i);
    }

    if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
      System.out.println(str + " is a Palindrome String.");
    }
    else {
      System.out.println(str + " is not a Palindrome String.");
    }
  }
}

Enter fullscreen mode Exit fullscreen mode

Output:
Radar is a palindrome.

Example

Using stack

import java.util.Scanner;
import java.util.Stack;

public class Main 
{
  public static void main(String[] args) 
  {
    System.out.print("Enter the string : ");
    Scanner in = new Scanner(System.in);
    String origString = in.nextLine();

    Stack<Character> stack = new Stack<>();

    //Push all chars in stack
    for (int i = 0; i < origString.length(); i++) {
      stack.push(origString.charAt(i));
    }

    String reverseString = "";

    //Pop all chars from stack one by one and build reverse string
    while (!stack.isEmpty()) {
      reverseString = reverseString + stack.pop();
    }

    //Check palindrome string
    if (origString.equals(reverseString)) {
      System.out.println("String is a palindrome.");
    } else {
      System.out.println("String is not a palindrome.");
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Output:
Enter the string: madam
String is a palindrome.

Example

Using queue

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main 
{
  public static void main(String[] args) 
  {
    System.out.print("Enter the string : ");
    Scanner in = new Scanner(System.in);
    String origString = in.nextLine();

    Queue<Character> queue = new LinkedList<>();

        for (int i = origString.length()-1; i >=0; i--) {
            queue.add(origString.charAt(i));
        }

    String reverseString = "";

    //Pop all chars from stack one by one and build reverse string
    while (!queue.isEmpty()) {
      reverseString = reverseString + queue.remove();
    }

    //Check palindrome string
    if (origString.equals(reverseString)) {
      System.out.println("String is a palindrome.");
    } else {
      System.out.println("String is not a palindrome.");
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Output:
Enter the string: racecar
String is a palindrome.

Enter the string: java
String is not a palindrome.

Conclusion

In this blog, we took in the string palindrome in the Java issue. We had examined two methodologies for the string palindrome in Java issue with the clarification of existence intricacy.

We truly want to believe that you are delighted in perusing this blog. Evaluate additional string issues in Java, similar to switch a string, check in the event that a number is a palindrome, check in the event that two given strings are isomorphic to one another, base characters to be added at front to make string palindrome, and so forth. In java there are multiple concepts that can be learned at initial and mid level to get expertise in java. Some of the concepts like final keywords in java, javascript basics, oops concepts etc.

Top comments (0)