Question

write a program in java that asks the user for a sentence, and then returns that...

write a program in java that asks the user for a sentence, and then returns that string with the words backwards. For example: Enter a sentence! Mary had a little lamb. Your sentence backwards: lamb. little a had Mary Write a method called reverse() that accepts a sentence as a string, and then returns that string with the words backwards. Write a main() method that gets the string from the user and then displays the string backwards. demonstrate program by writing comments

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

Enter b sentencel ary hsd a little Lno a T-en nt n n-tho: rm11 r.ver, fune on to rev.r. Packag tor key board resd ublic class ievesesntenee ast to first ofth array puhlic static Strirc reverstring str) t string arr[]-str.”11t(* ..); puhlie statir mid rai(serin rgt canner new Sce-ner(systen.in Syster, out, pr1rt:Enter ฮ sentence! -); Call sethod to disply reverssd sentenc

---------------------------------

Program

/*
* This program is to reverse a sentence
* Here user input a sentence
* Then main method call reverse function to reverse the sentence
*/
//Package for key board read
import java.util.Scanner;

public class ReverseSentence {
   /*
   * Method to reverse the string
   * Here we split the string using space and store into an array
   * Then from last to first of the array values concatenate with initialized empty string with space
   * return the reversed string
   */
   public static String reverse(String str) {
       String arr[]=str.split(" ");
       str="";
       for(int i=arr.length-1;i>=0;i--) {
           str+=arr[i]+" ";
       }
       return str;
   }
//Test method
   public static void main(String[] args) {
       //Scanner object open for keyboard read
       Scanner sc=new Scanner(System.in);
       //Prompt for sentence
       System.out.print("Enter a sentence! ");
       //Read sentence
        String inputSentence=sc.nextLine();
        //Call method to display reversed sentence
        System.out.println(reverse(inputSentence));
        //Close the scanner object
        sc.close();
   }

}

-------------------------------------------

Output

Enter a sentence! Mary had a little lamb.
lamb. little a had Mary

Add a comment
Know the answer?
Add Answer to:
write a program in java that asks the user for a sentence, and then returns that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ( IN JAVA): Write a program that reads each line in a file, and writes them...

    ( IN JAVA): Write a program that reads each line in a file, and writes them out in reverse order into another file. This program should ask the user for the name of an input file, and the name of the output file. If the input file contains: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go Then the output file should end up containing: The lamb...

  • Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and...

    Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: o Write a method called cubeIt that accepts one integer parameter and returns the value raised to the third power as an integer. 2. Write a method called randomInRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. o Write a method called larger that accepts two double parameters and...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • (JAVA NetBeans) Write a Java program, which asks the user to enter a string, then (a)...

    (JAVA NetBeans) Write a Java program, which asks the user to enter a string, then (a) reverse the string, (b) change the case of the string (CaT cAt) (c) print the chars at even positions (Object Ojc)

  • Write a C Program that asks the user to input a string and then the user...

    Write a C Program that asks the user to input a string and then the user is prompted back whether the input string is a “Palindrome Word” or not. (A Palindrome Word reads the same backward or forward, eg. madam, racecar, etc.) Your program should contain a function that accepts the input string from the main function and returns a value indicating whether the input string is a Palindrome or not. Use Pointers to traverse the string.

  • Write a program in java that asks a user for a file name and prints the...

    Write a program in java that asks a user for a file name and prints the number of characters, words (separated by whitespace), and lines in that file. Then, it replaces each line of the file with its reverse. For example, if the file contains the following lines (Test1.txt): This is a test Hi there Output on the console: Number of characters: 22 Number of words: 6 Number of lines: 2 Data written to the file (Test1.txt): tset a si...

  • Write a program that stores a phrase as an array of words, and then prints it...

    Write a program that stores a phrase as an array of words, and then prints it backwards. The main method calls method getInput , which asks the user how many words there are, stores them in an array, and returns this array. printBackwards then takes this array of words, and prints it in reverse. Code Example: import java.util.Scanner; public class L17Num1{    public static void main(String[] args) {    String[] sArray=getInput(); printBackwards(sArray); } public static String[] getInput() { System.out.println("How many...

  • PYTHON PROGRAM Write an application that accepts a sentence as input from the user and displays...

    PYTHON PROGRAM Write an application that accepts a sentence as input from the user and displays all of the words in the sentence that have an odd number of letters in them

  • LANGUAGE: JAVA Sorting an array: 2. Write a program that asks the user for 5 numbers...

    LANGUAGE: JAVA Sorting an array: 2. Write a program that asks the user for 5 numbers and stores them in an array called data. Then call a method that accepts the array as an argument, sorts the array in ascending order and prints out the original AND sorted array (Hint: The method does not return anything in this case, so it should be set to void).

  • Can you write with comments please. Thank you. Write a Java Program that asks the user...

    Can you write with comments please. Thank you. Write a Java Program that asks the user to enter the responses on the Console and write the responses to a text file called responses.txt (in the same directory as the program) Write another JAVA prorgram to read responses.txt file and calculate the frequency of each rating in the file and output the number and frequency in two columns (number, frequency) on the Console.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT