1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Ber [7]
4 years ago
6

Write a recursive method that tests whether a string is a palindrome. It should return a boolean T or F depending on whether or

not the string is a palindrome,
Computers and Technology
1 answer:
Sveta_85 [38]4 years ago
8 0

Answer:

// program in java.

// library

import java.util.*;

// class definition

class Main

{

// recursive function to check palindrome

   public static boolean isPalin(String str){

// base case

     if(str.length() == 0 ||str.length()==1){

        return true;

     }

// if first character is equal to last

     if(str.charAt(0) == str.charAt(str.length()-1))

     {

     // recursive call

        return isPalin(str.substring(1, str.length()-1));

     }

     // return

     return false;

  }

  // main method of the class

public static void main (String[] args) throws java.lang.Exception

{

   try{

    // object to read input

Scanner scr=new Scanner(System.in);

System.out.print("Enter a string:");

 // read string from user

String myString =scr.nextLine();

 // call function to check palindrome

     if (isPalin(myString)){

        System.out.println("Given String is a palindrome");

     }

// if string is not palindrome

     else{

        System.out.println("Given String is not a palindrome");

     }    

   }catch(Exception ex){

       return;}

}

}

Explanation:

Read a string from user and assign it to variable "myString" with scanner object. Call the method isPalindrome() with string input parameter.In this method, if  length of string is 0 or 1 then it will return true(base case) otherwise it will call itself and compare first character with last character.If string is palindrome then function will return true else return false.

Output:

Enter a string:ababa                                                                                                      

Given String is a palindrome

You might be interested in
The statements in the body of a while loop may never be executed, whereas the statements in the body of a do-while loop will be
LenKa [72]

Answer:

a. at least once

Explanation:

A loop is a code snippet that uses a condition to run repeatedly for multiple times which could be zero, one or more times. As long as the condition is true, the loop statement executes over and over again. Three common types of loop are;

i. for loop

ii. while loop

iii. do while loop

In a for or while loop, the condition for the loop is first tested before the statements in the loop are executed. Statements in the body of the loop are executed zero or more times. i.e the loop statement (statement in the body of the loop) may or may not be executed.

In a do..while loop, the statements in the loop are executed first before the condition of the loop is tested. In this case, the statements in the body of the loop are executed one or more times. i.e the loop statement is executed at least once.

6 0
3 years ago
State two ways of preventing virus attack on a computer.​
Goshia [24]

Answer:

updating the computer and having an anti virus on your computer

5 0
3 years ago
Larry sent an email to Andy. Andy didn't open Larry's email but still understood what the message was. How did Andy determine th
Sladkaya [172]

Andy knew what the message was because of the subject you are required to add in almost all email sights.

7 0
3 years ago
Using a Windows computer, to make a window wider, you would move the pointer until it changes to the Horizontal Resize shape and
weqwewe [10]
Press and hold the left button on the mouse and drag window until it is the size youwant
4 0
3 years ago
1. Bus bars are used in an electric iron as A. connectors between the thermostat and the element. B. electrical terminals. C. th
katrin2010 [14]
Answer - A (Connectors between the thermostat and the element)

Electrical busbars usually conduct electrical energy and <span>transmits power from tan input feeder to an output feeder. In the case of an electric iron, the input feeder is the thermostat while the output feeder is the heating element present in the iron.</span>
5 0
3 years ago
Other questions:
  • All of the following statements correctly describe an advantage or disadvantage associated with the use of Monte Carlo Analysis
    9·1 answer
  • A decrease in Government Expenditures results in _________.
    15·1 answer
  • What is ATX full size motherboard? Tell its size as well?
    14·1 answer
  • Mention<br>any<br>5<br>indicators of<br>happiness​
    11·2 answers
  • What is one expectation of open-source software?
    15·1 answer
  • Most Muslims are Sunni Muslims; however, the Shiites are a forceful minority in some parts of the Middle East. The Sunni-Shiite
    13·1 answer
  • Which musical instrument would have the lowest pitch ?
    12·1 answer
  • When both inputs of a J-K edge-triggered FF are high and the clock cycles, the output will ________.
    11·1 answer
  • suppose as a head software engineer you assign the job of creating a class to a subordinate. You want to specify thirty-eight di
    11·1 answer
  • Describe ONE similarity and ONE difference between the use of lighting in candid and posed photography.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!