The project name of this exercise is Reverse word The purpose of this assignment
ID: 3778183 • Letter: T
Question
The project name of this exercise is Reverse word The purpose of this assignment is to learn how to work with iteration and string processing. You get more practice on how to write all of your own code and Javadoc comments. Problem Description This problem is inspired by the book's Problem E 6.9 on page 299. You are going to write program that takes a word (or short phrase) from the command line (i. e. the input is in) and then will reverse the letters in the word. Getting started There is no code to copy for the assignment. You get to do it all ! Don't forget to provide proper Javadoc documentation. We are going to do this exercise by writing the object that solves the problem by putting code into reverse word. Java. Using the techniques shown on the web page titled [http://crowd. Cs. Sbcc.edu: 7990/projects/CS 105F 2016/repos/allan. Kinght/browse/How to: to create a source file called Reverse Word. Java. This is where your code will go. Testing Your Code Your Reverse word. Java should contain code for your solution to this problem. A sample run, assuming is passed on the command line is shown below:Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
import java.util.Scanner;
public class ReverseWord {
// function to revere word
public static String reverse(String str){
String rev = "";
for(int i=0; i<str.length(); i++)
rev = str.charAt(i) + rev;
return rev;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
// taking user input
System.out.println("Enter a string: ");
String str = sc.nextLine();
// splitting string by space
String[] arr=str.split(" ");
StringBuilder sb=new StringBuilder();
// reversing each word
for(int i=arr.length-1;i>=0;i--)
{
// reversing current word and appending in result
sb.append(reverse(arr[i]));
sb.append(" ");
}
System.out.print(sb);
}
}
/*
Sample run:
Enter a string:
THis is a word
drow a si siHT
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.