Answer:
// here is code in java.
// import package
import java.util.*;
// class definition
class Main
{
// method that return reverse string
public static String reverseO(String st,int n)
{
// create an empty string
String rev_st="";
for(int x=n-1;x>=0;x--)
{
// append character from last into the empty string
rev_st=rev_st+st.charAt(x);
}
// return the reversed string
return rev_st;
}
// main method of the class
public static void main (String[] args) throws java.lang.Exception
{
try{
// scanner object to read string
Scanner scr=new Scanner(System.in);
System.out.print("enter the string:");
// read the string from user
String st=scr.nextLine();
// find the length of string
int len=st.length();
// call the method with parameter string and length
System.out.println("reverse string is: "+reverseO(st,len));
}catch(Exception ex){
return;}
}
}
Step-by-step explanation:
Read a string from user and assign it to variable "st" and Find its length. Then call the method reverseO() with parameter string and length.Then in the method reverseO(), create an empty string.Then append the characters of input string to empty string from last index.Then return the string.This will be the string in reverse order.
Output:
enter the string:hello
reverse string is: olleh