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
Question
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
Solution
Sure, here is a simple Java program that takes a user input string, reverses it, and then checks if it is a palindrome.
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 input, reverse it, and convert it back to a String.
- We compare the reversed word with the original word, ignoring case differences.
- If they are equal, we print that the word is a palindrome. Otherwise, we print that it is not a palindrome.
Similar Questions
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.
Your friend is in university and there is a coding challenge going on. A string S of lowercase letters is displayed over the screen. The length of the string is N. An integer M is also displayed over the screen.Your task is write a program to determine if we can make the string Palindrome by performing at most M operations on the provided string
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 python program to check the given string is palindrome or not.
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); } } }}
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.