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));
}
}
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);
}
}

Execution sample:

JAVA Write a method that accepts a String as an argument. The method should use recursion...
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: " +...
instructions These questions should test if you have understood the contents of Chapter 18 "Recursion". 1. What will this method return if you call it this: xMethod (4) static int xMethod (int n) { if (n == 1) return 1; else return n + xMethod (n - 1); } 1. 10 2. 11 3. 12 4. 9 2. Analyze the following code: public class Test { public static void main (String [] args) { int [] x = {1, 2,...
Given the following code: public static void foo3(String s) { if (s.length() >0) { System.out.print(s.charAt(s.length() -1)); foo3(s.substring(0, s.length() -1)); } } What is the output of: foo3(“”); 2, You coded the following in the file Test.java : System.out.println( foo(5)); //more code here public static int foo(int n) //line 9 { if (n = = 0) return 1; else System.out.println(n* foo(n-1) ); } //line 15 At compile time, you get the following error: Text.java: 15: missing return statement } ...
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 =...
Debug following methods(longestRun(finds how many times a character got repeated), findLastP(finds last p in a string), findFirstP(finds first p in a string)) in java language. public class simpleLoops { /** * @param args */ public static void main(String[] args) { System.out.println(longestRun("aabbbccd")); System.out.println("Expected 3"); System.out.println(longestRun("aaa")); System.out.println("Expected 3"); System.out.println(longestRun("aabbbb")); System.out.println("Expected 4"); int count = countP("Mississippi"); System.out.println(count); int result = findLastP("Mississippi"); System.out.println(result); result = findFirstP("stop"); System.out.println(result); result = findFirstP("xxxyyyzzz"); System.out.println(result); } /** *...
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 (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 "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, 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 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...