Some in-class development Design Problem Consider these definitions: number, lis
ID: 3796962 • Letter: S
Question
Some in-class development Design Problem Consider these definitions: number, list These are recursive definitions, The thing being defined uses its own definition. Recursion is a powerful problem- solving technique that can simplity solutions to certain types of problems. The ability to use recursion rests on whether the programming language supports it, which depends on how the method invocations are handled. method ca activation record Writing recursive solutions requires care. Three things must be provided: base case recursive call with a "smaller" instance ofthe problem general case (inductive case) Using recursion may be just one way of solving a problem, and may not always be the most efficient way. For the program we are going to develop, recursion is a natural way to approach its solution. Write a program to determine whether a text phrase input by the user is a palindrome. A palindrome is a phrase that reads the same forward and backward. Madam, I'm Adam. A man, a plan, a canal, Panama! A rough outline of what needs to be done: 1. Input text string to be tested 2. Pre-process the string to ignore punctuation, and turn all characters to uppercase 3. Send pre-processed string to a Boolean method which returns true ifthe string is a palindrome, otherwise return false he solution is fairly short. No special structures are needed, other than strings. e: 2/28/17; NAA 3/2/17Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
public class chegg
{
public static boolean isPal(String s)
{
if(s.length() == 0 || s.length() == 1) // if length is 0 or 1 then String is palindrome
return true;
if(s.charAt(0) == s.charAt(s.length()-1)) // check for first and last char of String
return isPal(s.substring(1, s.length()-1));
return false; // If program control reaches to this statement it means the String is not palindrome hence return false.
}
public static void main(String[]args)
{
Scanner in = new Scanner(System.in); //For capturing user input
System.out.println("Enter the String for check:");
String strin = in.nextLine();
String str = strin.replaceAll("[^a-zA-Z0-9]", ""); //Remove all non-alphanumeric characters
str = str.toUpperCase(); // Turn the string to upper case
if(isPal(str)) //If function returns true then the string is palindrome else not
System.out.println(strin + " is a palindrome");
else
System.out.println(strin + " is not a palindrome");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.