In: Computer Science
First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects).
Then create a new Java application called "BackwardsStrings" (without the quotation marks) that:
So, for example, if the first string is 'usr' and the second string is 'bin', your program would output something like the following:
The two strings you entered are: usr bin. The two strings in reverse are: nib rsu.
Note that the reversed SECOND string comes FIRST when printing the strings in reverse.
This program must be completed without using loops and without using StringBuilder.
See Horstmann pp. 62-63 for some ideas. Make sure your program includes the command line prompts for the user and that it formats the output appropriately.
Be sure both input strings are three characters in length. If this data validation isn't passed, output "Invalid string length for one or both inputs." and do not proceed with further processing of the strings.
import java.util.Scanner;
public class BackwardsStrings {
public static void main(String[] args) {
// TODO Auto-generated method
stub
Scanner sc=new
Scanner(System.in);
System.out.println("Enter 3
character string");
String str1=sc.nextLine();
System.out.println("Enter next 3
character string");
String str2=sc.nextLine();
if(str1.length()== str2.length()
&& str1.length()==3){
System.out.println("The two strings you entered are:"+str1+"
"+str2);
System.out.println("The two strings in reverse
are:"+getReverse(str2,str2.length()-1)+" "+
getReverse(str1,
str1.length()-1));
}
else{
System.out.println("Invalid string length for one or both
inputs.");
}
}
public static String getReverse(String s, int
pos){
String rev="";
if(pos==0){
return
String.valueOf(s.charAt(pos));
}
rev=s.charAt(pos)+getReverse(s, pos-1);
return rev;
}
}
Expected output: