Question

JAVA Write a method that accepts a String as an argument. The method should use recursion...

JAVA

Write a method that accepts a String as an argument. The method should use recursion to display each individual character in the String. Then, modify the method you just wrote so it displays the String backwards.

The following code does not correctly display the String backwards. It merely moves the first character of the String to the end:

public static void displayCharacter(String s)
   {
       if(s.length() == 0)
           return;
       else
       {
           System.out.println(s.charAt(0));
           displayCharacter(s.substring(1));
       }
   }
  
   public static void displayCharacterReverse(String s)
   {
       if(s.length() == 0)
           return;
       else
       {
           displayCharacter(s.substring(1));
           System.out.println(s.charAt(0));
       }
   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

There is just a slight error in displayCharacterReverse, while recursing on the input string, displayCharacterReverse function needs to call itself and not displayCharacter.

Correct Code:

public class display_string{
public static void displayCharacter(String s){
if(s.length() == 0)
return;
else{
System.out.println(s.charAt(0));
displayCharacter(s.substring(1));
}
}
  
public static void displayCharacterReverse(String s){
if(s.length() == 0)
return;
else{
displayCharacterReverse(s.substring(1));
System.out.println(s.charAt(0));
}
}

public static void main(String a[]){
String string = new String("Hello World!");
System.out.println("Printing string : "+string);
System.out.println("Printing characters in original order:");
displayCharacter(string);
System.out.println("Printing characters in reverse order:");
displayCharacterReverse(string);
}
}

X display_string.java i public class display_string{ 2 public static void displayCharacter(String s){ 3 if(s.length() == 0) 4

Execution sample:

Printing string : Hello world! Printing characters in original order: H OPP I พ 1 d ! Printing characters in reverse order: !

Add a comment
Know the answer?
Add Answer to:
JAVA Write a method that accepts a String as an argument. The method should use recursion...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** *...

    Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** * Program starts with this method. * * @param args A String to be printed backwards */ public static void main(String[] args) { if (args.length == 0) { System.out.println("ERROR: Enter a String on commandline."); } else { String word = args[0]; String backwards = iterativeBack(word); // A (return address) System.out.println("Iterative solution: " + backwards); backwards = recursiveBack(word); // B (return address) System.out.println("\n\nRecursive solution: " +...

  • Write a static method called printWithSpaces that takes a String as its parameter and prints the...

    Write a static method called printWithSpaces that takes a String as its parameter and prints the characters of the string separated by spaces. For example: > Methods.printWithSpaces("method") m e t h o d You should have a single space after the last character. This method should not return a value. That is similar to this code for printing the string vertically public static void printVertical(String s) { for (int i = 0; i < s.length(); i++) { char c =...

  • The code below accepts and evaluates an integer expression with the following operators: +, _, *,...

    The code below accepts and evaluates an integer expression with the following operators: +, _, *, and /. Your task is to modify it to include the % remainder operator that has the same precedence as * and /. No need to rewrite the entire program, just insert the needed statements. import java.util.Stack; public class EvaluateExpression { public static void main(String[] args) {     // Check number of arguments passed     if (args.length != 1) {       System.out.println(         "Usage:...

  • 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (...

    10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println () 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ]...

  • In Java This is the method we did in class. How do I reverse the string...

    In Java This is the method we did in class. How do I reverse the string "monday"? If I could get the string "onday" reversed (to produce "yadno" then all I have to do is append the first character to make "yadnom". import java.util.Scanner; public class Lab12Num2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine(); System.out.println("The string entered was " + s); System.out.println("The string printed backwards is "+ printBackwards(s)); }    public static String printBackwards(String one)...

  • WRITTEN IN JAVA Complete the method printIndices that accepts a String source and a character search,...

    WRITTEN IN JAVA Complete the method printIndices that accepts a String source and a character search, and prints the position of each occurrence of the search value in source. For example: Input: source = "String Programming in Java is fun", search = 'i' Console Output: 3 15 19 27 Template provided from question:    public static int printIndices(String source, char search)    {    }    public static void main(String[] args)    {        System.out.println("Expected printIndices() prints indices 3...

  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

    Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...

  • Java 7 Write a constructor for the MightyByte class so it can accept a String argument...

    Java 7 Write a constructor for the MightyByte class so it can accept a String argument and set the bits field. Complete the for loop in the getDecimalValue method. class Main { public static void main(String[] args) { MightyByte myByte = new MightyByte("00000101"); System.out.println(myByte.getDecimalValue());    } } class MightyByte { private String bits;    // write the constructor here    public int getDecimalValue() { int decimalValue = 0; int intValue; int power = 7; for (int i = 0; i...

  • 10. What prints when the following code is executed? public static void main (String args) "Cattywampus";...

    10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println ()

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT