This is the first solution to check whether a given string is palindrome or not. This approach uses a simple predicate with lambda expression, and uses StringBuffer class method.
import java.util.Scanner;
import java.util.function.Predicate;
public class PalindromeCheck{
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner inpStringVar= new Scanner(System.in);
/* Define a Predicate to check whether String is Palindrome using Lambda expression */
Predicate<String> palinStr= (strVal) -> {
StringBuffer strBuf=new StringBuffer(strVal);
strBuf.reverse();
return (strVal.contentEquals(strBuf));
};
//Input a string to check for Palindrome
System.out.print("Enter String or Alpha numeric value: ");
String inpStr=inpStringVar.next();
System.out.println("The string: "+inpStr+" is Palindrome: "+palinStr.test(inpStr));
}
}
Output:
Enter String or Alpha numeric value=
MalayalaM
The string: MalayalaM is Palindrome: true

Leave a comment