1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
4vir4ik [10]
3 years ago
6

(Displaying a Sentence with Its Words Reversed) Write an application that inputs a line of text, tokenizes the line with String

method split and outputs the tokens in reverse order. Use space characters as delimiters.

Computers and Technology
1 answer:
Serjik [45]3 years ago
4 0

Answer:

I am writing a JAVA and Python program. Let me know if you want the program in some other programming language.

import java.util.Scanner; // class for taking input from user

public class Reverse{ //class to reverse a sentence

public static void main(String[] args) { //start of main() function body

Scanner input = new Scanner(System.in); //create a Scanner class object

   System.out.print("Enter a sentence: "); //prompts user to enter a sentence

   String sentence = input.nextLine(); // scans and reads input sentence

   String[] tokens = sentence.split(" "); // split the sentence into tokens using split() method and a space as delimiter

   for (int i = tokens.length - 1; i >= 0; i--) { //loop that iterates through the tokens of the sentence in reverse order

       System.out.println(tokens[i]); } } } // print the sentence tokens in reverse order

Explanation:

In JAVA program the user is asked to input a sentence. The sentence is then broken into tokens using split() method. split method returns an array of strings after splitting or breaking the sentence based on the delimiter. Here the delimiter used is space characters. So split() method splits the sentence into tokens based on the space as delimiter to separate the words in the sentence. Next the for loop is used to iterate through the tokens as following:

Suppose the input sentence is "How are you"

After split() method it becomes:

How

are

you

Next the loop has a variable i which initialized to the tokens.length-1. This loop will continue to execute till the value of i remains greater than or equals to 0.

for (int i = tokens.length - 1; i >= 0; i--)

The length of first token i.e you is 3 so the loop executes and prints the word you.

Then at next iteration the value of i is decremented by 1 and now it points at the token are and prints the word are

Next iteration the value of i is again decremented by i and it prints the word How.

This can be achieved in Python as:

def reverse(sentence):

   rev_tokens = ' '.join(reversed(sentence.split(' ')))

   return rev_tokens

   

line = input("enter a sentence: ")

print((reverse(line)))

The method reverse takes a sentence as parameter. Then rev_tokens = ' '.join(reversed(sentence.split(' ')))  statement first splits the sentence into array of words/tokens using space as a delimiter. Next reversed() method returns the reversed sentence.  Next the join() method join the reversed words of the sentence using a space separator ' '.join(). If you want to represent the reversed tokens each in a separate line then change the above statement as follows:

rev_tokens = '\n'.join(reversed(sentence.split(' ')))  

Here the join method joins the reversed words separating them with a new line.

Simply input a line of text and call this reverse() method by passing the input line to it.

You might be interested in
A database planner names a field “Organic ingredients_3”. Why would this name Create possible problems in programming?
o-na [289]

Answer:

I think the answer is 2.

hope it will help you

4 0
3 years ago
Read 2 more answers
Is my document folder located at taskbar​
KiRa [710]
Nope its located at your my computer
8 0
3 years ago
The phenomenon of transforming a somewhat stable soil into mobile material capable of rising toward Earth's surface is called __
Naya [18.7K]

Answer: Liquefaction

Explanation:

  The phenomenon of generate the liquid from the solid or gases in accordance with the dynamics fluid is known as liquefaction. It basically occur either naturally or artificially.  It is the process of transforming the stable soil into the mobile material is known as liquefaction.

The liquefaction is the process that basically occur due to the vibration and the water pressure in the surface and that cause the soil particle to loose the contact in the soil.

This can be prevented by the avoiding the bearing capacity from the liquefaction and also by control the seismic settlement in the process.

6 0
4 years ago
What is a Windows feature that allows you to temporarily store text?
nata0808 [166]
This would be copy and paste.

When you select a text, you can use your keys CTRL and C to copy that same selected text. After this, you can use the paste feature by selecting CTRL and V. Doing this, your selected text will be copied and pasted to a certain location. 
3 0
3 years ago
The term asymptotic means the study of the function f as n becomes larger and larger without bound.
harkovskaia [24]

Answer:

False

Explanation:

The term asymptotic means approaching a value or curve arbitrarily closely

5 0
3 years ago
Other questions:
  • To create a default value for a parameter in the parameter list of a function, you code a/an ________________ sign after the par
    5·1 answer
  • Although highly accurate navigational information from the GPS constellation is exploitable by adversary forces, it is unlikely
    5·1 answer
  • Which sns gets its names from one of its unique features
    15·1 answer
  • A user's computer will not boot. A technician notices four indicator lights on the backside of the
    8·1 answer
  • Tim is trying to explain to his aunt how her computer works, but she does not understand what a CPU does. Which description will
    11·1 answer
  • Eliza needs to share contact information with another user, but she wants to include only certain information in the contact. Wh
    7·1 answer
  • How are special characters added to a Word document? Check all that apply.
    11·2 answers
  • You wish to use a file system that creates a record or log of to-be-committed changes in the system so that if the system crashe
    14·1 answer
  • Which of the following is a document view you can select using Button on the status bar?
    11·1 answer
  • The output of a combiner is normally fed into a
    10·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!