In: Computer Science
in. java
Write a program that reads a string from the user, and creates another string with the letters from in reversed order. You should do this using string concatenation and loops. Do not use any method from Java that does the work for you. The reverse string must be built. Do not just print the letters in reversed order. Instead, concatenate them in a string.
--- Sample run: This program will create a string with letters in reversed order. Enter a string: banana The reversed string is: ananab Bye --- Sample run: This program will create a string with letters in reversed order. Enter a string: what the heck The reversed string is: kceh eht tahw Bye
//Java Program
import java.util.Scanner;
public class Reverse{
public static void main(String args[]){
String input;
Scanner in =new Scanner(System.in);
System.out.println("This program will create a string with letters
in reversed order.");
System.out.print("Enter a string:");
input = in.nextLine();
System.out.println("The reversed string
is:"+reverseString(input));
System.out.println("Bye");
in.close();
}
public static String reverseString(String str){
String newStr="";
for(int i=str.length()-1;i>=0;i--){
newStr+=str.charAt(i);
}
return newStr;
}
}
//sample output