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...

  • 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...

  • 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.

  • In Java, write a program that prompts the user to input a sequence of characters and...

    In Java, write a program that prompts the user to input a sequence of characters and outputs the number of vowels. You will need to write a method to return a value called isAVowel which will return true if a given character is a vowel and returns false if the character is not a vowel. A second method, called isAConstant, should return true if a given character is a constant and return false if the character is not a constant....

  • 1. String Length Write a function that returns an integer and accepts a pointer to a...

    1. String Length Write a function that returns an integer and accepts a pointer to a C-string as an argument. The function should count the number of characters in the string and return that number. Demonstrate the function in a simple program that asks the user to input a string, passes it to the function, and then displays the function’s return value. c++

  • Write a program that asks the user for his or her name, and then asks the user to enter a sentence that describes him or herself.

    23. Personal Weeb Page GeneratorWrite a program that asks the user for his or her name, and then asks the user to enter a sentence that describes him or herself. Here is an example of the program's screen:Enter your name: Julie Taylor [Enter]<html>  <head>  </head>  <body>    <center>      <h1>Julie Taylor</h1>    </center>    <hr />    I am a computer science major, a member of the Jazz club,    and I hope to work as a mobile app developer after I graduate.    <hr />  </body> </html>Describe yourself: I am a computer science major, a member of theOnce the user has entered the requested input, the program should create an HTML file,...

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
ADVERTISEMENT