Write a Java program to check whether a given word is a palindrome or not. for e.g. MADAM is a palindrome but eat is not.
Question
Write a Java program to check whether a given word is a palindrome or not. for e.g. MADAM is a palindrome but eat is not.
Solution 1
Sure, here is a simple Java program that checks if a given word is a palindrome or not:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a word:");
String word = scanner.nextLine();
String reversedWord = new StringBuilder(word).reverse().toString();
if (word.equalsIgnoreCase(reversedWord)) {
System.out.println(word + " is a palindrome.");
} else {
System.out.println(word + " is not a palindrome.");
}
}
}
Here's how it works:
- We import the Scanner class which is used to get user input.
- We create a new instance of the Scanner class.
- We prompt the user to enter a word.
- We read the user's input using the
nextLine()method of the Scanner instance. - We create a new StringBuilder instance with the user's word, reverse it, and convert it back to a String.
- We compare the reversed word with the original word using the
equalsIgnoreCase()method. This method returns true if the two strings are equal, ignoring case considerations. - If the original word and the reversed word are equal, we print that the word is a palindrome. Otherwise, we print that the word is not a palindrome.
Solution 2
Sure, here is a simple Java program that checks if a given word is a palindrome or not:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a word: ");
String word = scanner.nextLine();
String reversedWord = new StringBuilder(word).reverse().toString();
if(word.equalsIgnoreCase(reversedWord)) {
System.out.println(word + " is a palindrome.");
} else {
System.out.println(word + " is not a palindrome.");
}
}
}
Here's how it works:
- We import the Scanner class which is used to get user input.
- We create a new instance of the Scanner class.
- We ask the user to enter a word.
- We store the user's input in the
wordvariable. - We reverse the
wordusing theStringBuilderclass and itsreversemethod, and convert it back to a string. - We compare the original word with the reversed word using the
equalsIgnoreCasemethod. This method returns true if the two strings are equal, ignoring case considerations. - If the original word and the reversed word are equal, we print that the word is a palindrome. Otherwise, we print that the word is not a palindrome.
Solution 3
Sure, here is a simple Java program that checks if a given word is a palindrome or not:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a word: ");
String word = scanner.nextLine();
String reversedWord = new StringBuilder(word).reverse().toString();
if(word.equalsIgnoreCase(reversedWord)) {
System.out.println(word + " is a palindrome.");
} else {
System.out.println(word + " is not a palindrome.");
}
}
}
Here's how it works:
- We import the Scanner class which is used to get user input.
- We create a new instance of the Scanner class.
- We prompt the user to enter a word.
- We read the user's input using the
nextLine()method of the Scanner instance. - We create a new StringBuilder instance with the user's word, reverse it and convert it back to a string.
- We compare the reversed word with the original word using the
equalsIgnoreCase()method which ignores the case when comparing strings. - If the original word and the reversed word are equal, we print that the word is a palindrome. Otherwise, we print that the word is not a palindrome.
Similar Questions
class Solution { public boolean isPalindrome(int x) { String num =Integer.toString(x); for(int i=num.length()-1;i<0;i--){ String a =" "+num; if(num==a){ System.out.println("it is palindrome:"+a); }else{ System.out.println("it is not palindrome:"+a); } } }}
Write a python program to check the given string is palindrome or not.
With the Module on Java Characters, Strings, and the StringBuilder that I have provided in our Messenger GC and in Teams. Do this Laboratory Activity:Write a Java program that takes a user input string, reverses it, and then checks if it is a palindrome. Sample Words to check: civic, radar, level, rotor, kayak, madam, and refer
You are provided with a code to find if a given string is a palindrome or not.The code is shuffled. Rearrange the code in the correct order.System.out.println(str+" is not a Palindrome");import java.util.Scanner;StringBuffer sb = new StringBuffer(str);public static void main(String[] args) {Scanner sc=new Scanner(System.in);public class Main {elseString str=sc.next();sb.reverse();if(str.equalsIgnoreCase(reverse)) }}System.out.println(str+" is a Palindrome");String reverse=sb.toString();
Write a program in java to accept a number and chech whether it is a palindrome number or not. Do the program without using modulus operator and String.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.