I wrote a code to test if the inputted string is a palindrome, but I need to rem
ID: 3876255 • Letter: I
Question
I wrote a code to test if the inputted string is a palindrome, but I need to remove the caps, spacing, and punctuation. I can't seem to figure out how to make my code work.
import java.util.Scanner; class Palindromef public static void main (String args[1)f Scanner kb - new Scanner (System.in); System.out.print ("Enter a word or sentence:") String line - kb.nextLine () isReverse (line); public static boolean isReverse (String line) string newLine = '' '' ; String reve r seLine '' '' ; line = line. toLowerCase() ; for (int i = 0; i = 11ne.length() - I; i++ if (Character.isLetter(line.charAt (i))) newLine += line . charAt (i) ; for (int 1-newLine . length ( )-1; i-0; i--) reverseLine += newLine . charAt (i); if (line.equals (reverseLine)) System.out.println(" Input string is a palindrome."); return true else System.out.println ("Input string is not a palindrome."); return false;Explanation / Answer
CheckPalindrome.java
import java.util.Scanner;
public class CheckPalindrome {
public static void main(String[] args) {
//Declaring variables
String str, stripped = "", reversed;
boolean bool;
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
System.out.print("Enter a String :");
str = sc.nextLine();
//Finding the Stripped String
for (int i = 0; i < str.length(); i++) {
if ((str.charAt(i) >= 'a' && str.charAt(i) <= 'z') || (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')) {
if (Character.isUpperCase(str.charAt(i))) {
stripped += (char)(str.charAt(i) + 32);
} else {
stripped += str.charAt(i);
}
}
}
//Displaying the Stripped String
System.out.println("Stripped :" + stripped);
//calling the method by passing the Stripped String as input
reversed = reverse(stripped);
//Displaying the Reversed String
System.out.println("Reversed :" + reversed);
// Calling the method by passing the Stripped string and Reversed String as input.
bool = testPalindrome(stripped, reversed);
// based on the bool value the corresponding message will be displayed
if (bool) {
System.out.println("This is a Palindrome.");
} else {
System.out.println("This is not a Palindrome.");
}
}
/* Method implementation which Reverse the Stripped String
* @Params : String
* @return : String
*/
private static String reverse(String stripped) {
String reverse = "";
for (int i = stripped.length() - 1; i >= 0; i--) {
reverse = reverse + stripped.charAt(i);
}
return reverse;
}
/*
* This method will check whether the string is palindrome or not If the
* String is Palindrome then it returns true If not,It returns false
* Param:string return: boolean
*/
private static boolean testPalindrome(String stripped, String reversed) {
/*
* This for loop will compare each character in the user entered string
* and the reversed string
*/
for (int i = 0; i < stripped.length(); i++) {
if (stripped.charAt(i) != reversed.charAt(i))
return false;
}
return true;
}
}
______________________
Output#1:
Enter a String :"Campus motto: Bottoms up, Mac!"
Stripped :campusmottobottomsupmac
Reversed :campusmottobottomsupmac
This is a Palindrome.
__________________________
Output#2:
Enter a String :helloworld
Stripped :helloworld
Reversed :dlrowolleh
This is not a Palindrome.
_____________Could you rate me well.Plz .Thank You