201k views
4 votes
Write a recursive function

string reverse(string str)
that computes the reverse of a string. For example, reverse("flow") should return "wolf". Hint: Reverse the substring starting at the second character, then add the first character at the end. For example, to reverse "flow", first reverse "low" to "wol", then add the "f" at the end.

1 Answer

0 votes

Answer:

Step-by-step explanation:

The following code is written in Java. It creates a function called stringReverse that takes in one string parameter and uses a recursive algorithm to print out the input string in reverse. The output can be seen in the attached picture below.

void stringReverse(String str)

{

//Check to see if String is only one character or less

if ((str==null) || (str.length() <= 1))

System.out.println(str);

else

{

System.out.print(str.charAt(str.length()-1));

stringReverse(str.substring(0,str.length()-1));

}

}

Write a recursive function string reverse(string str) that computes the reverse of-example-1
User Zyonneo
by
4.9k points