Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need to be answered correctly. You must: Comment and Format and Must make sure a

ID: 3876542 • Letter: N

Question

Need to be answered correctly. You must: Comment and Format and Must make sure about indenting. If you don't follow these steps, it will be flagged for review. MUST BE IN JAVA FORMAT. IT IS A MUST. Please also check the output very carefully. Must not provide the answer on a page. Use the Java compiler. Please provide the code in such a way so that it can be copied and checked in the Java compiler. Must provide the coding with three outputs in total and different than the one provided. One output must show palindrome and the rest two output must show not palindrome.

Need to be answered correctly. You must: Comment and Format and Must make sure about indenting. If you don't follow these steps, it will be flagged for review. MUST BE IN JAVA FORMAT. IT IS A MUST. Please also check the output very carefully. Must not provide the answer on a page. Use the Java compiler. Please provide the code in such a way so that it can be copied and checked in the Java compiler. Must provide the coding with three outputs in total and different than the one provided. One output must show palindrome and the rest two output must show not palindrome.

Exercise 2: Write a program that accepts a String as input from the keyboard and determines whether or not the String is a palindrome. Ignore whitespace and punctuation (space, tab, single quote, double quote, exclamation, comma, question mark, hyphen, semi-colon, colon, period). Ignore case as well. For example,all these inputs are palindromes: A man, a plan, a canal, Panama Was it a car or a cat I saw? "If I had a hi-fi!!" Use the following template: public class Palindrome public static void main(StringLl args)f //code here public static boolean isReverse (String line) //code here ladd other methods if required

Explanation / Answer

Palindrome.java

import java.util.Scanner;

public class Palindrome {

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();

//calling the method by passing the Stripped String as input

reversed = reverse(stripped);

// Calling the method by passing the Stripped string and Reversed String as input.

bool = isReverse(str);

// 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 isReverse(String str) {

String stripped ="",reversed;

//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);

}

}

}

  

reversed=reverse(stripped);

/*

* 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:

Enter a String :"Campus motto: Bottoms up, Mac!"
This is a Palindrome.

__________________

Output#1:

Enter a String :"If I had a hi-fi!!"
This is a Palindrome.

_______________Could you plz rate me well.Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote