| by Arround The Web | No comments

How to Check if a String is a Palindrome in Java

While dealing with data in Java, there can be the possibility of the contained palindrome data. This data makes it convenient for the developer to access these values since the index is identical from start and end. In such cases, checking if a string is a palindrome in Java does wonders in utilizing and sorting the data conveniently.

This blog will guide the approaches to checking for a palindrome string in Java.

What is a Palindrome?

A string is referred to as a “palindrome” if it is the same pronounced and written reversely as by default.

Example

  • Dad
  • Pop
  • level

How to Check/Verify if a String is a Palindrome in Java?

To check if a string is a palindrome in Java, apply the “for” loop in combination with the “charAt()” and the “equals()” methods. The “for” loop is used to iterate along the elements one by one, the “charAt()” method gives the character at the specified index within a string, and the “equals()” method compares the two strings.

Syntax

charAt(int index)

In the above syntax, “ind” points to the index of the corresponding element that needs to be referred.

equals(str)

In this syntax, “str” refers to the string that needs to be compared.

Example 1: Check if the Specified String is a Palindrome in Java

In this example, the specified string can be checked for “Palindrome”:

String givenString = "Pop";

String place = "";

for (int i= (givenString.length()-1);i>=0;i--) {

place= place + givenString.charAt(i);

}

if (givenString.toLowerCase().equals(place.toLowerCase())) {

System.out.println("The string is a Palindrome");

}

else {

System.out.println("The string is not a Palindrome");

}

According to the above code, apply the following steps:

  • Initialize the “String” value to be checked for “Palindrome”.
  • In the next step, define another “String” to accumulate the string in reverse order.
  • Now, apply the “for” loop to iterate through the specified string reversely via the “length” property.
  • Note that “1” is subtracted from the string’s length since the index initiates from “0”.
  • After that, store the iterated values in the allocated “String” variable via the “charAt()” method.
  • Lastly, apply the condition such that the given string and the reversed string both are “equal” via the combined “toLowerCase()” and “equals()” methods and display the corresponding message.
  • Note that the former method is applied to display the palindrome string irrespective of the “case sensitivity”.

Output

In the above output, it can be observed that the specified string is a palindrome, irrespective of the fact that the string contains both the upper case and lower case values.

Example 2: Check if the User Input String is a Palindrome in Java

In this particular example, a user input string can be evaluated for “Palindrome”:

import java.util.Scanner;

Scanner scanner= new Scanner(System.in);

System.out.println("Enter the string");

String input= scanner.nextLine();

if (checkPalindrome(input)) {

System.out.print("The string is palindrome"); }

else {

System.out.print("The string is not a palindrome"); }

scanner.close(); }

public static Boolean checkPalindrome(String givenString) {

String place = "";

for (int i= (givenString.length()-1);i>=0;i--) {

place= place + givenString.charAt(i);

}

if (givenString.toLowerCase().equals(place.toLowerCase())) {

return true; }

else {

return false;

}

In this code block, apply the following steps:

  • First of all, include the “Scanner” class to get the string from the user that needs to be checked for “Palindrome” and “System.in” reads the input string.
  • After that, invoke the function “checkPalindrome()” and pass the input string as its argument.
  • Since the function returns the “boolean” value. Therefore, upon the function being “true”, the former condition will be executed. Otherwise, the latter condition will come into effect.
  • Now, define the function named “checkPalindrome()” having the stated parameter referring to the passed string.
  • In the function definition, recall the discussed approaches for iterating through the passed string reversely and placing it in a separate string.
  • After that, similarly, verify if the given and the reversely iterated strings are “equal” and return the corresponding boolean value based on that.

Output

In this output, it can be analyzed that the user-defined strings are evaluated accordingly.

Conclusion

To check if a string is a “palindrome” using Java, apply the “for” loop in combination with the “charAt()” and the “equals()” methods. These approaches can be utilized to apply a check upon the specified and user input string values irrespective of case sensitivity, respectively. This blog discussed the approaches to verifying if a string is a palindrome using Java.

Share Button

Source: linuxhint.com

Leave a Reply