We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
oart2023 20:97 Check fa String Is a Palndrome in Java | Baclsung
”
Check if a String Isa
Palindrome in Java
es
5 méthodes utiliser pour la
probléme
= fabric
(httos://ads.freestarcom/?
Last updated: October 5, 2023,
Written by: baeldung (https://www.baeldung.com/author/baeldung)
hitpsitwwwbaeldung.conviava-paindrome 9oart2023 20:97 Check fa String Is a Palndrome in Java | Baclsung
dova String “atips: //ww, baeldung.com/category/java/java-string)
Get started with Spring 5 and Spring Boot 2,
through the Learn Spring course:
>> CHECK OUT THE COURSE (/\s-course-start)
1. Introduction
In this article, we're going to see how we can check whether a given String is
a palindrome using Java
Apalindrome is a word, phrase, number, or other sequences of characters
which reads the same backward as forward, such as ‘madam’ or ‘racecar’
2. Solutions
In the following sections, welll look at the various ways of checking if a given
Stringis a palindrome or not.
2.1. A Simple Approach
We can simultaneously start iterating the given string forward and
backward, one character at a time. If the there is a match the loop continues:
otherwise, the loop exits:
hitpsitwwwbaeldung.conviava-paindrome 219oart2023 20:97 Check fa String Is a Palndrome in Java | Bacleung
wublic bo.' 220 rave (stAing text) { CJ
String clean = text.replaceall("\\s#", "™"),toLowerCase();
‘int length = clean. length();
int forward = 0;
int backward = length - 1;
while (backward > forward)
char forwardChar = clean.charAt(forward++) ;
char backwardChar = clean. charAt(backward-
if (forwardChar backwardChar)
return false;
}
return true;
2.2. Reversing the String
There are a few different implementations that fit this use case: we can
make use of the API methods from StringBuilderand StringBuffer classes
when checking for palindromes, or we can reverse the String without these
classes,
‘on prams saneron SharePoint On-Premises vs
‘SHAREPOINT ONLINE
Online : Lequel vous convie
?
$ Powell Software
(https://ads.freestarcom/?
ce
Let's take a look at the code implementations without the helper APIs first:
hitpsitwwwbaeldung.conviava-paindrome
a9oart2023 20:97 Check fa String Is a Palndrome in Java | Bacleung
wublic bo.’ rv oneRevétseTheString(String text) { CJ
StringBuilder reverse = new StringBuilder () ;
String clean = text.replaceAll("\\s+", '""). toLowerCase();
char[] plain = clean. toCharArray() ;
for (int i = plain.length - 1; i >=; i--) {
reverse. append(plain[i]);
3
return (reverse. toString()) .equals(clean) ;
In the above snippet. we simply iterate the given String from the last
character and append each character to the next character, all the way
through to the first character thereby reversing the given String.
Finally, we test for equality between the given String and reversed String.
The same behavior could be achieved using API methods.
Let's see a quick demonstration:
Offre borne de recharge
“Hyundai Motor France
(https://ads.freestarcom/?
public boolean isPalindromeUsingStringBuilder (string text) { CJ
String clean = text.replaceAll("\\s+", "").toLowerCase() ;
StringBuilder plain = new StringBuilder (clean) ;
StringBuilder reverse = plain.reverse();
return (reverse. toString()) .equals (clean) ;
+
public boolean isPalindromeUsingStringBuffer (string text) {
String clean = text.replaceall("\\st", ""). toLowerCase() ;
StringBuffer plain = new StringBuffer (clean) ;
StringBuffer reverse = plain.reverse();
return (reverse. toString()) .equals(clean) ;
hitpsitwwwbaeldung.conviava-paindrome 49cast2008 2037 Cceckifa Sting Ia Palnérome Java | Baeeung
In the code srippet. we invoke the, reverse”) method from the StringBuilder
ain Stringévuffer AMI 10 reverse the given String and test for equality
2.3. Using Stream API
We can also use an /ntStream to provide a solution:
public boolean isPalindromeUsingIntStream(String text) { CE]
String temp = text.replaceAll("\\s#", "").toLowerCase() ;
return IntStream.range(9, temp.length() / 2)
-NoneMatch(i -> temp.charAt(i) != temp.charAt(temp.length() -
i-4))5
+
In the snippet above, we verify that none of the pairs of characters from
each end of the String fulfills the Predicate condition
2.4, Using Recursion
Recursion is a very popular method to solve these kinds of problems. In the
example demonstrated we recursively iterate the given String and test to
find out whether it's a palindrome or not:
public boolean isPalindromeRecursive(String text) { CJ
String clean = text.replaceAll("\\s#", '™").toLowerCase();
return recursivePalindrome(clean,®,clean.length()-1);
}
private boolean recursivePalindrome(String text, int forward, int
backward) {
if (forward == backward) {
return true;
}
if ((text.charat(forward)) != (text.charAt(backward))) {
return false;
}
if (forward < backward + 1) {
return recursivePalindrome(text, forward + 1, backward -
1)5
}
return true;
hitpsitwwwbaeldung.conviava-paindrome 59oart2023 20:97 Check fa String Is a Palndrome in Java | Baclsung
3. Cone! us
In this quick tutorial. we saw how to find out whether a given Stringis a
palindrome or not
As always, the code examples for this article are available over on GitHub
(https://github.com/eugenp/tutorials/tree/master/core-java-
modules/core-java-string-algorithms)
Get started with Spring 5 and Spring Boot 2,
through the Learn Spring course:
>> CHECK OUT THE COURSE (/\s-course-end)
hitpsitwwwbaeldung.conviava-paindrome asCheck fa String Is a Palndrome in Java | Baclsung
Learning to build your API
with Spring?
Download the E-book (/rest-api-spring-guide)
3 COMMENTS, 5 & Oldest ~
View Comments
Comments are closed on this article!
ips:fmaew-baeldung.comjava-palindrome 719oart2023 20:97 Check fa String Is a Palndrome in Java | Baclsung
7)
COURSES
ALL COURSES (/ALL-COURSES)
ALL BULK COURSES (/ALL-BULK-COURSES)
ALL BULK TEAM COURSES (/ALL-BULK-TEAM-COURSES)
THE COURSES PLATFORM (H7TPS:// COURSES BAELOUNG.COM)
SERIES.
JAVA “BACK 10 BASICS' TUTORIAL (VJAVA-TUTORIAL)
JACKSON JSON TUTORIAL (/JACKSON)
APACHE HTTPCLIENT TUTORIAL (/HTTPCLIENT-GUIDE)
REST WITH SPRING TUTORIAL (/REST-WITH-SPRING-SERIES)
SPRING PERSISTENCE TUTORIAL (/PERSISTENCE-WITH-SPRING-SERIES)
SECURITY WITH SPRING (/SECURITY-SPRING)
SPRING REACTIVE TUTORIALS (/SPRING-REACTIVE-GUIDE)
ABOUT
ABOUT BAELDUNG (/ABOUT)
THE FULL ARCHIVE (/FULL-ARCHIVE)
EDITORS (/EDITORS)
JOBS (/TAG/ACTIVE-J03/)
ips:fmaew-baeldung.comjava-palindrome asoart023 2037
OUR PARTNEPS (/PARTNERS)
PARTNER W!.H eazcoUNe var vERTICR)
Check fa String Is a Palndrome in Java | Baclsung
TERMS OF SERVICE (/TERMS-OF-SERVICE)
PRIVACY POLICY (/PRIVACY-POLICY}
COMPANY INFO (/BAELDUNG-COMPANY-INFO)
CONTACT (/CONTACT)
ips:fmaew-baeldung.comjava-palindrome a9