How can I write a code to output the same result as this code but using recursio
ID: 3937898 • Letter: H
Question
How can I write a code to output the same result as this code but using recursion in java.
public class Recursion{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a String");
String input = keyboard.nextLine();
int len = input.length();
char[] tempCharArray = new char[len];
for(int i = 0; i < len; i++)
{
tempCharArray[i] = input.charAt(i);
System.out.println(tempCharArray[i]);
}
}
}
Explanation / Answer
Hi, Please find my recursive code.
Please let me know in case of any issue.
import java.util.Scanner;
public class Recursion{
public static void recursion(char[] tempCharArray, int i, String input){
// base case
if(i == tempCharArray.length)
return;
tempCharArray[i] = input.charAt(i);
System.out.println(tempCharArray[i]);
recursion(tempCharArray, i+1, input);
}
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a String");
String input = keyboard.nextLine();
int len = input.length();
char[] tempCharArray = new char[len];
// calling recursive function
recursion(tempCharArray, 0, input);
}
}
/*
Sample run:
Enter a String
this is the string
t
h
i
s
i
s
t
h
e
s
t
r
i
n
g
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.