Java program to check if string is a palindrome : check palindrome string in java

Blogs ❯❯ Java

Image could not load

Image on pixabay

What is palindrome string

palindrome string / words वो words होते हैं जिन्हे किसी भी side से read करने पर same ही रहते हैं। या कह सकते हैं कि अगर आप string के letters को reverse कर दें तो same ही रहेंगे।

इस article में Java language में हम palindrome string check करने के लिए एक program बनाएंगे।

Java program to check if string is a palindrome

// import package. import java.util.*; public class PalindromeExample { // define function to check Palindrome string public static boolean isPalindrome(String str) { int n = str.length(); for (int i = 0; i < n / 2; i++) { if (str.charAt(i) != str.charAt(n-i-1)) { return false; } } return true; } public static void main(String[] args) { //System.in is a standard input stream Scanner sc= new Scanner(System.in); System.out.print("Enter a string: "); String str= sc.nextLine(); if (isPalindrome(str)) { System.out.println(str + " is a palindrome"); } else { System.out.println(str + " is not a palindrome"); } } }

Output :

Enter a string: hello
hello is not a palindrome

Enter a string: level
level is a palindrome
Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.

Get connected with me. :) LinkedIn Twitter Instagram Facebook