recursion Problems: 1)I need help changing my Boolean method from a loop to usin
ID: 671284 • Letter: R
Question
recursionProblems: 1)I need help changing my Boolean method from a loop to using recursion 2)My program only works for one word palindromes, it needs to work for phrases like Desserts, I stressed. I tried writing a statment that cuts out (, . ;), but that hasn't worked.
Please help me fix those two issues. This is the assignment prompt: A palindrome is any word, phrase, or sentence that reads the same forwards and backwards. Write a Boolean method that uses recursion to determine whther a String argument is a palindrome. The method should return true if the argument reads the same forwards and backward. Demonstrate the method in a program.
This is my program:
import java.util.Scanner;
/**
*
* @author Jocelyn
*/
public class Program_6JL {
/**
* @param args the command line arguments
*/
/*
This program uses takes a String and reverses it. The code
uses recursion to see if a string is a palindrome. If the String is a palindrome
then the message "The word is a palindrome" will be diplayed. If the word isn't a
palindrome, then the message "The word isn't a palindrome" will appear.
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//getting phrase or word
System.out.print("Please type in a phrase or word: ");
//storing phrase or word
String myString = keyboard.nextLine();
//will return true if palindrome, false if not
boolean palindrome = isPalindrome(myString);
if(palindrome==true)
System.out.println("The word is a palindrome");
else
System.out.println("The word isn't a palindrome");
}// end of main
public static boolean isPalindrome(String input) {
//to store palindrome
String secondString = "";
//checks if phrase is a palindrome
for (int a = 0; a < 1; a++) {
if (input.length() > 0) {
secondString = reverseString(input);
}
}//end of palindrome loop
if(input.equals(secondString)){
System.out.println(secondString);
return true;
}
else
return false;
}//end of isPalindrome method
public static String reverseString(String s) {//reverses string
String t = "";
String remove = ",";
String replaceWith = " ";
//removes any white space, converts letters to lowercase
s = s.toLowerCase().trim().replaceAll(remove, replaceWith);
//reverses string and sends it back
for (int i = s.length() - 1; i > -1; i--) {
t = t + s.charAt(i);
}
return t;//sends back string in reverse
}//end of reverseString method
}//end of public class recursion
Problems: 1)I need help changing my Boolean method from a loop to using recursion 2)My program only works for one word palindromes, it needs to work for phrases like Desserts, I stressed. I tried writing a statment that cuts out (, . ;), but that hasn't worked.
Please help me fix those two issues. This is the assignment prompt: A palindrome is any word, phrase, or sentence that reads the same forwards and backwards. Write a Boolean method that uses recursion to determine whther a String argument is a palindrome. The method should return true if the argument reads the same forwards and backward. Demonstrate the method in a program.
This is my program:
import java.util.Scanner;
/**
*
* @author Jocelyn
*/
public class Program_6JL {
/**
* @param args the command line arguments
*/
/*
This program uses takes a String and reverses it. The code
uses recursion to see if a string is a palindrome. If the String is a palindrome
then the message "The word is a palindrome" will be diplayed. If the word isn't a
palindrome, then the message "The word isn't a palindrome" will appear.
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//getting phrase or word
System.out.print("Please type in a phrase or word: ");
//storing phrase or word
String myString = keyboard.nextLine();
//will return true if palindrome, false if not
boolean palindrome = isPalindrome(myString);
if(palindrome==true)
System.out.println("The word is a palindrome");
else
System.out.println("The word isn't a palindrome");
}// end of main
public static boolean isPalindrome(String input) {
//to store palindrome
String secondString = "";
//checks if phrase is a palindrome
for (int a = 0; a < 1; a++) {
if (input.length() > 0) {
secondString = reverseString(input);
}
}//end of palindrome loop
if(input.equals(secondString)){
System.out.println(secondString);
return true;
}
else
return false;
}//end of isPalindrome method
public static String reverseString(String s) {//reverses string
String t = "";
String remove = ",";
String replaceWith = " ";
//removes any white space, converts letters to lowercase
s = s.toLowerCase().trim().replaceAll(remove, replaceWith);
//reverses string and sends it back
for (int i = s.length() - 1; i > -1; i--) {
t = t + s.charAt(i);
}
return t;//sends back string in reverse
}//end of reverseString method
}//end of public class recursion
Problems: 1)I need help changing my Boolean method from a loop to using recursion 2)My program only works for one word palindromes, it needs to work for phrases like Desserts, I stressed. I tried writing a statment that cuts out (, . ;), but that hasn't worked.
Please help me fix those two issues. This is the assignment prompt: A palindrome is any word, phrase, or sentence that reads the same forwards and backwards. Write a Boolean method that uses recursion to determine whther a String argument is a palindrome. The method should return true if the argument reads the same forwards and backward. Demonstrate the method in a program.
This is my program:
import java.util.Scanner;
/**
*
* @author Jocelyn
*/
public class Program_6JL {
/**
* @param args the command line arguments
*/
/*
This program uses takes a String and reverses it. The code
uses recursion to see if a string is a palindrome. If the String is a palindrome
then the message "The word is a palindrome" will be diplayed. If the word isn't a
palindrome, then the message "The word isn't a palindrome" will appear.
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//getting phrase or word
System.out.print("Please type in a phrase or word: ");
//storing phrase or word
String myString = keyboard.nextLine();
//will return true if palindrome, false if not
boolean palindrome = isPalindrome(myString);
if(palindrome==true)
System.out.println("The word is a palindrome");
else
System.out.println("The word isn't a palindrome");
}// end of main
public static boolean isPalindrome(String input) {
//to store palindrome
String secondString = "";
//checks if phrase is a palindrome
for (int a = 0; a < 1; a++) {
if (input.length() > 0) {
secondString = reverseString(input);
}
}//end of palindrome loop
if(input.equals(secondString)){
System.out.println(secondString);
return true;
}
else
return false;
}//end of isPalindrome method
public static String reverseString(String s) {//reverses string
String t = "";
String remove = ",";
String replaceWith = " ";
//removes any white space, converts letters to lowercase
s = s.toLowerCase().trim().replaceAll(remove, replaceWith);
//reverses string and sends it back
for (int i = s.length() - 1; i > -1; i--) {
t = t + s.charAt(i);
}
return t;//sends back string in reverse
}//end of reverseString method
}//end of public class Problems: 1)I need help changing my Boolean method from a loop to using recursion 2)My program only works for one word palindromes, it needs to work for phrases like Desserts, I stressed. I tried writing a statment that cuts out (, . ;), but that hasn't worked.
Please help me fix those two issues. This is the assignment prompt: A palindrome is any word, phrase, or sentence that reads the same forwards and backwards. Write a Boolean method that uses recursion to determine whther a String argument is a palindrome. The method should return true if the argument reads the same forwards and backward. Demonstrate the method in a program.
This is my program:
import java.util.Scanner;
/**
*
* @author Jocelyn
*/
public class Program_6JL {
/**
* @param args the command line arguments
*/
/*
This program uses takes a String and reverses it. The code
uses recursion to see if a string is a palindrome. If the String is a palindrome
then the message "The word is a palindrome" will be diplayed. If the word isn't a
palindrome, then the message "The word isn't a palindrome" will appear.
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//getting phrase or word
System.out.print("Please type in a phrase or word: ");
//storing phrase or word
String myString = keyboard.nextLine();
//will return true if palindrome, false if not
boolean palindrome = isPalindrome(myString);
if(palindrome==true)
System.out.println("The word is a palindrome");
else
System.out.println("The word isn't a palindrome");
}// end of main
public static boolean isPalindrome(String input) {
//to store palindrome
String secondString = "";
//checks if phrase is a palindrome
for (int a = 0; a < 1; a++) {
if (input.length() > 0) {
secondString = reverseString(input);
}
}//end of palindrome loop
if(input.equals(secondString)){
System.out.println(secondString);
return true;
}
else
return false;
}//end of isPalindrome method
public static String reverseString(String s) {//reverses string
String t = "";
String remove = ",";
String replaceWith = " ";
//removes any white space, converts letters to lowercase
s = s.toLowerCase().trim().replaceAll(remove, replaceWith);
//reverses string and sends it back
for (int i = s.length() - 1; i > -1; i--) {
t = t + s.charAt(i);
}
return t;//sends back string in reverse
}//end of reverseString method
}//end of public class
Explanation / Answer
package mani;
import java.util.Scanner;
/**
*
* @author Jocelyn
*/
public class Program_6JL {
/**
* @param args the command line arguments
*/
/*
This program uses takes a String and reverses it. The code
uses recursion to see if a string is a palindrome. If the String is a palindrome
then the message "The word is a palindrome" will be diplayed. If the word isn't a
palindrome, then the message "The word isn't a palindrome" will appear.
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//getting phrase or word
System.out.print("Please type in a phrase or word: ");
//storing phrase or word
String myString = keyboard.nextLine();
System.out.println("Reverse of the string: "+reverseString(myString));
//will return true if palindrome, false if not
boolean palindrome = isPalindrome(myString);
if(palindrome==true)
System.out.println("The word is a palindrome");
else
System.out.println("The word isn't a palindrome");
}
public static boolean isPalindrome(String s)
{
if(s.length() == 0 || s.length() == 1)
return true;
if(s.charAt(0) == s.charAt(s.length()-1))
return isPalindrome(s.substring(1, s.length()-1));
return false;
}
// end of main
/*public static boolean isPalindrome(String input) {
//to store palindrome
String secondString = "";
//checks if phrase is a palindrome
for (int a = 0; a < 1; a++) {
if (input.length() > 0) {
secondString = reverseString(input);
}
}//end of palindrome loop
if(input.equals(secondString)){
System.out.println(secondString);
return true;
}
else
return false;
}//end of isPalindrome method
*/
public static String reverseString(String s) {//reverses string
String t = "";
String remove = ",";
String replaceWith = " ";
//removes any white space, converts letters to lowercase
s = s.toLowerCase().trim().replaceAll(remove, replaceWith);
//reverses string and sends it back
for (int i = s.length() - 1; i > -1; i--) {
t = t + s.charAt(i);
}
return t;//sends back string in reverse
}//end of reverseString method
}//end of public class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.