Question

In: Computer Science

Write a RECURSIVE method that receives a string as a parameter. The method will return true...

Write a RECURSIVE method that receives a string as a parameter. The method will return true if the string received as a parameter is a palindrome and false if it is not.

The method must not have any loops!

In JAVA

Solutions

Expert Solution

The answer to this question is as follows:

The code is as follows:

import java.util.*;
public class Palindrome
{
   static boolean isPal(String str,int start, int end)
   {
      
       if (start == end)
           return true;

         
       if ((str.charAt(start)) != (str.charAt(end)))
           return false;

         
       if (start < end + 1)
           return isPal(str, start + 1, end - 1);

       return true;
   }

   static boolean checkPalindrome(String str)
   {
       int len = str.length();
       if (len == 0)
           return true;

       return isPal(str, 0, len - 1);
   }

     
   public static void main(String args[])
   {
   //I am providing the input as the static
       String str = "madam";
       //If you want the input as the dynamic please comment above line and uncomment below two line
       //Scanner sc=new Scanner(System.in);
       //String str=sc.nextLine();
System.out.println(checkPalindrome(str));
         
   }
}

The input and output are provided in the screenshot below:



Related Solutions

Write a RECURSIVE method that receives 2 strings as parameters. The method will return true if...
Write a RECURSIVE method that receives 2 strings as parameters. The method will return true if the 2nd string is a subsequence of the 1st string. If not, the method will return false. An empty string is a subsequence of every string. This is because all zero characters of the empty string will appear in the same relative order in any string This method must not contain any loops. In java
Write a RECURSIVE method that receives as a parameter an integer named n. The method will...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will output n # of lines of stars. For example, the first line will have one star, the second line will have two stars, and so on. The line number n will have "n" number of ****** (stars) so if n is 3 it would print * ** *** The method must not have any loops!
} 1. write a function that takes a string as parameter, return true if it’s a...
} 1. write a function that takes a string as parameter, return true if it’s a valid variable name, false otherwise. You can use keyword module’s  iskeyword() to determine is a string is keyword. import keyword keyword.iskeyword(var)   #returns true or false }2. write a function that returns the length of a string (without using len() function) }3. write a function that counts number of vowels in a string }4. write a function that checks if a word is palindrome PYTHON PROGRAMMING
1. Write a method called isPalindrome that accepts a string as a parameter and returns true...
1. Write a method called isPalindrome that accepts a string as a parameter and returns true if the string is a palindrome otherwise returns false. This method uses a stack and a Queue to test whether the given string parameter is a palindrome [ that is, whether the characters read the same both forward and backward. For example “race car”, and “Are we not drawn onward, to new era.” are Palindromes] They are palindrome sentences, not just a word. 2....
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
Create a method called firstLetter that takes a String parameter and integer parameter. It should return...
Create a method called firstLetter that takes a String parameter and integer parameter. It should return -1 if the number of words in the given String is greater than or equal to the integer parameter (it should return -1 and not process the String any further) (4 points). If the String does not have more words than the given integer, the method should return 1 if all the words in the string start with the same letter(8 points) and 0...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the...
Write recursive method to return true if a given array has element equal to employee emp,...
Write recursive method to return true if a given array has element equal to employee emp, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary, // and it has method boolean equals(Employee emp) that accept an employee object and returns true if employee calling the equals method is equal as employee emp, and returns...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT