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 ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook