Fill in the gap to complete the recursive function below so that it splits the characters of the input parameter into separate lines.
The new line character in Java is \n
public static String lemon(String s){
if(s.length() == 0 || str.length() ==1 ) return s;
return___________________________
}
Following is the required line :
return (s.charAt(0)+ "\n" + lemon(s.substring(1)));
Following is the complete code :
import java.util.Scanner;
public class SplitCharacters {
public static void main(String[] args) {
System.out.print("Enter a string : ");
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
scanner.close();
String splitString = lemon(s);
System.out.println(splitString);
}
public static String lemon(String s) {
if(s.length() == 0 || s.length() == 1) {
return s;
}
return (s.charAt(0)+ "\n" + lemon(s.substring(1)));
}
}
Following are some sample runs :
Enter a string : first
f
i
r
s
t
Enter a string : test string
t
e
s
t
s
t
r
i
n
g
Enter a string : ba
b
a
Get Answers For Free
Most questions answered within 1 hours.