
import java.util.Scanner;
class Main {
public static boolean isPalindrome(String str, int start, int end) {
if (start >= end) {
return true;
} else if (str.charAt(start) == str.charAt(end)) {
return isPalindrome(str, start + 1, end - 1);
} else {
return false;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
do {
System.out.println("Please enter a string to test for palindrome or type QUIT to exit:");
str = sc.nextLine().toLowerCase();
if (str.equals("quit")) {
break;
} else {
if (isPalindrome(str, 0, str.length() - 1)) {
System.out.println("The input is a palindrome.");
} else {
System.out.println("The input is not a palindrome.");
}
}
} while (true);
}
}
Test Case 1
Desserts, I stressedENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
KayakENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Test Case 2
dadENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Test Case 3
momENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
QuitENTER
Test Case 4
helloENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quiTENTER
Test Case 5
b,a,dENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Test Case 6
Able was I, ere I saw ElbaENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Test Case 7
A man, a plan, a canal, PanamaENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Test Case 8
@abENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
ab@ENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
@aaENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
aa@ENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Test Case 9
abbaENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
abcaENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
aabcaaENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
abaceedabaENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER



Step by stepSolved in 3 steps with 3 images

- Given a long string use recursion to traverse the string and replace every vowel (A,E,I,O,U) with an @ sign. You cannot change the method header. public String encodeVowels(String s){ // Your code here }arrow_forwardWrite a static recursive method in Java called mRecursion that displays all of the permutations of the charactersin a string passed to the method as its argument. For example, the character sequence abc has thefollowing permutations: acb, bac, bca, cab, cba. Then Write a static method called getInput that get aninput string from the user and passed it to the mRecursion method written above in a method call.Please does so using what I already had //Get input from in to call recursive method //to display those char permutations public static String getInput ( ) { Scanner in = new Scanner(System.in); String combination = in.nextLine(); System.out.println("Enter string:); return stringComb; //Method to show permutations of a desired string// This only return 3 string combination for some reason static void myRecursion(String aString) { //isEmpty check if ( aString.length() == 0){…arrow_forwardJava - Write an iterative method that calculates the SUM of all integers between 1 and a given integer N (input into the method). Write a corresponding recursive solution. (return answer, don’t print)arrow_forward
- Write a recursive method, matchingParen (String str), that returns: true if str is a nesting of zero or more pairs of parentheses, like "())" or "((()". false if str is null, or if str has any character other than right or left parentheses, or if the right and left parentheses do not match. System.out.println( matchingParen( ) ); System.out.println( matchingParen( "(())" ) ); System. out.println( matchingParen( "(()))" ) ); System.out.println( matchingParen( "((x))" ) ); System.out.println( matchingParen( "()))") ); System.out.println( matchingParen( null ) ); //true //true //true //false //false //false Hint: You may check the first and last chars, and then recur on what's inside them. You may also find it easier to use a helper method.arrow_forwardI need help creating a code below - Write a recursive method called digitCount() that takes a positive integer as a parameter and returns the number of digits in the integer. Hint: The number of digits increases by 1 whenever the input number is divided by 10. Ex: If the input is: 345 the method digitCount() returns and the program outputs: 3arrow_forwardWrite a Non tail recursion and a tail recursion method in Java with a test class that does the following: The factorial of a positive integer n —which we denote as n!—is the product of n and the factorial of n 1. The factorial of 0 is 1. Write two different recursive methods in Java that each return the factorial of n. Please and Thank youarrow_forward
- The factorial of a positive integer n —which we denote as n!—is the product of n and the factorial of n 1. The factorial of 0 is 1. Write two different recursive methods in Java that each return the factorial of n. Analyze your algorithm in Big-Oh notation and provide the appropriate analysis, ensuring that your program has a test class.arrow_forwardWrite a recursive method oddSum that takes a positive odd integer n and returns the sum of odd integers from 1 to n.arrow_forwardjavaarrow_forward
- Use java and correctly indent code.arrow_forwardI have two questions: Is my programs code the most effective way at finding recursion or is there a much better and cleaner way? Why is the program showing 0 when I implement a number that depth is greater than 100? Shouldn't it show the number 1? Source code: package recursiveModule; public class RecursiveMethod { public static long calculateFactorial(int n, int depth) { if (depth > 100 || n <= 1) { return 1; } return n * calculateFactorial(n - 1, depth + 1); } public static void main(String[] args) { System.out.println("Factorial of 1 is " + RecursiveMethod.calculateFactorial(1, 0)); System.out.println("Factorial of 8 is " + RecursiveMethod.calculateFactorial(8, 0)); System.out.println("Factorial of 101 is " + RecursiveMethod.calculateFactorial(101, 0)); } }arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





