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
Recommended Post:
- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data, POP, pop item, update method
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number
Get Salesforce Answers