Need help with this Java homework ASAP, thank you. Write a program that asks the
ID: 3850818 • Letter: N
Question
Need help with this Java homework ASAP, thank you.
Write a program that asks the user to enter a sentence. The program then passes the String into a method called convertString that changes the String to an array of chars and returns the array. The program then passes the array into a method called isReverse and returns true/false if the array of chars 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. --------amanaplanacanalpanama
Was it a car or a cat I saw? ----------wasitacaroracatlsaw
“If I had a hi-fi!!” ---------ifihadahifi
Use the following template:
import java.util.Scanner;
public class Ex1{
public static void main(String[] args){
//code here
}
public static char [] convertString (String s) {
//code here
}
public static boolean isReverse (char [] arr){
//code here
}
//add other methods if required
}
Explanation / Answer
Sample Code:
import java.util.*;
class HelloWorld
{
public static void main(String [] args)
{
String userStr="";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
userStr = in.nextLine(); //-- read user input.
char[] inputVal = convertString (userStr);//call convertString function and covert string to character.
System.out.println("Converted string is: ");
for(int i=0;i<inputVal.length;i++){
System.out.print(inputVal[i]);
}
if(isReverse (inputVal))
{
System.out.println(" It is a palindrome.");
}else{
System.out.println(" It is not a palindrome.");
}
}
/*
* Convert string to charater with some special cases.
*/
public static char [] convertString (String s) {
char[] chInput = s.toCharArray();
char[] convertedChar = new char[chInput.length];
int inc = 0;
for(int i=0;i<chInput.length;i++){
if(Character.isLetter(chInput[i])){ //-- check character is a letter/alphabets to avoid special characters
convertedChar[inc] = Character.toLowerCase(chInput[i]); //-- convert the character to lowercase and create a new char array
inc++;
}
}
return convertedChar;//-- return character array
}
public static boolean isReverse (char [] arr){
int length = arr.length;
char[] reverse = new char[arr.length];
System.out.println("Reversed string is: ");
for ( int i = length - 1; i >= 0; i-- ){ //-- create a char array and store the data in reverse order.
if(arr[i] != '')
{
reverse[i] = arr[i];
}
System.out.print(reverse[i]);
}
int flag = 1;
for(int i=0;i<length;i++){
if(arr[i] != '')
{
System.out.println(arr[i]);
System.out.println(reverse[i]);
if(arr[i] != reverse[i]) //-- check reversed char array and original char array are not same, if so set flag as 0 and exit from for loop.
{
System.out.println("sddfsdf");
flag = 0;
break;
}
}
}
if (flag==1) //if flag 1 then return true otherwise return false
return true;
else
return false;
}
}