Create a Java program using a NetBeans project containing a single class using a
ID: 3681332 • Letter: C
Question
Create a Java program using a NetBeans project containing a single class using an appropriate NetBeans project name and Java class name. The program will take a user input string, determine if the string is a palindrome (the same forwards and backwards) and display the determination. Details:
i. The program must take in a text input by the user.
1. The text can include letters, number, spaces and punctuation.
2. The input does not need to be error checked.
ii. The program must determine if the string is a palindrome.
1. The program must make its determination by creating a method that reverses the string and afterwards compares the original string to the reversed string. If the two are equal, the string is a palindrome.
2. The method to reverse the string must use the following header: a. String reverseStr(String s)
3. The program cannot use any library method to reverse the string, the reversal must be accomplished one character at a time.
iii. The program does not need to repeat.
iv. Sample program runs:
1. Not a palindrome: Enter text: abcde [Enter] Input String: abcde Reversed String: edcba String is not a palindrome
2. Palindrome: Enter text: abcba [Enter] Input String: abcba Reversed String: abcba String is a palidrome
Explanation / Answer
import java.util.*;
public class Palindrome{
public static void main(String args[]){
private String original;
private String 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("Enter text : "+original +" "+"Reversed String : "+reverse +"String is a palindrome");
else
System.out.println("Enter text : "+original +" "+"Reversed String : "+reverse +"String is not a palindrome");
}
}