Table of Contents
Java program to checks whether a given string is a palindrome or not
In this program, we learn to chacks whether a given string is a palindrome or not.
Algorithm: Palindrome or not in Java
- import java.util.*;
- Create: class Palind{
- Create main function: public static void main(String args[]){
- String original, reverse=””;
- Scanner in = new Scanner(System.in);
- Print message “Enter a string to check if it is a palindrome”
- Read string: original = in.nextLine();
- Find length: int length = original.length();
- for ( int i = length – 1 ; i >= 0 ; i– )
- reverse = reverse + original.charAt(i);
if (original.equals(reverse)) {
System.out.println(“Entered string is a palindrome.”);
else: System.out.println(“Entered string is not a palindrome.”);
Source Code: Palindrome or not
import java.util.*;
class Palind
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for (int i = length - 1; i >= 0; i--)
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
}
}
Expected Output:
Enter a string to check if it is a palindrome
Madam
Entered string is a palindrome

FAQ:
How do you reverse a string in Java?
To reverse a string: for (int i = length – 1; i >= 0; i–)
reverse = reverse + original.charAt(i);
How do you check if a given string is a palindrome Java?
To check a given string is a palindrome in java, we have to compare reverse with real string.
if (original.equals(reverse))
Recommended Post:
- Java roll dice 10000 times with algorithm and source code
- Write a Java program that displays the number of characters, lines, and words in a text
- Write a Java program that reads a file and displays the file on the screen with a line number before each line
- Write a Java program that reads a file name from the user, then displays information about whether the file exists, readable, writable, type of file, and the length of the file in bytes
- Java program to make frequency count of vowels, consonants, special symbols, digits, words in a given text
- Write a Java program for sorting a given list of names in ascending order
- Write a java program to Checks whether a given string is a palindrome or not
- Write a java program to perform multiplication of two matrices
- write a java program that prints the Fibonacci series for a given number.
Find the solution to the salesforce Question and many more
Tags: palindrome or not in java, palindrome or not