Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

sentence is \"hello!\" here is the code I\'m trying to reverse the sentence \"he

ID: 3635469 • Letter: S

Question

sentence is "hello!"
here is the code
I'm trying to reverse the sentence "hello!" to be
!olleh

Here is the code (I'm sure It is 70% correct but missing something)

int i=0;
public void reverse()
{
if(i>sentence.length())
{
return;
}
String firstone=sentence.substring(0,1);
String middlesent=sentence.substring(1,sentence.length());
sentence=middlesent+firstone;
i++;
System.out.println(sentence);
reverse();


}

this output


ello!h
llo!he
lo!hel
o!hell
!hello
hello!
ello!h


if u need the complete code let me know, thanx in advance

Explanation / Answer

please rate - thanks

how's this?

import java.util.*;
public class recursivereversestring
{static int i;
static String sentence;
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.print("Enter a string to reverse: ");
sentence=in.nextLine();
i=sentence.length()-1;
System.out.println("i="+i);
System.out.println(sentence+" reversed is: ");   
reverse();
    }
public static void reverse()
{i--;
if(i<0)
{
return;
}
String firstone=sentence.substring(0,i);
String middlesent=sentence.substring(i,i+1);
String endsent=sentence.substring(i+1,sentence.length());
sentence=firstone+endsent+middlesent;
System.out.println(sentence);
reverse();


}
}