Site icon Java Guidance

Java Program to Check Palindrome String using stream

PalindromStringUsingStream

In this example, we are going to learn how to find string is palindrome or not using stream.
Steps to solve this problem :
1. First we will iterate string by half of its size
2. We will compare each character so here we are comparing ith element with (length-i-1)th element if all matches then string is palindrom

Below example will return true if string is palindrom

import java.util.stream.IntStream;

public class PalindromeCheck {
public static void main(String[] args) {
String input = "madam";
boolean isPalindrome = IntStream.range(0, input.length() / 2)
.allMatch(i -> input.charAt(i) == input.charAt(input.length() - i - 1));

System.out.println("Is the string a palindrome? " + isPalindrome);
}
}
Exit mobile version