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 computer connected to the Internet that asks for data is a(n) ________. Select one: A. server B. client C. aggregator D. surro
madreJ [45]

Answer:

client

Explanation:

The client makes a request for a service, and a server performs that service.

7 0
4 years ago
Arrange the binary number in increasing order of their arithmetic output in the decimal number system ?
tiny-mole [99]
I think the order would be

10010001-1000001 (as the biggest amount)
100101100/101
10011x101
10010x11
10100+10101 (as the least amount)

hope this helps! :)
6 0
3 years ago
PLEASE HELP I WILL GIVE BRAINLIEST AND 100 POINTS IF U ANSWER COMPLETELY WITHIN 30 MIN
icang [17]

Answer:

Explanation:

be themselves and that is more than enough to be beautiful. Lastly, I would organize the colorson each photo to be organized to make it look like flowers. That would add a nice touch to seemlike the unorganized pictures are actually organized.

7 0
3 years ago
ORANGE COLORED SIGNS AND FLAGS MEAN THAT YOU MUST BE ALERT FOR:
Finger [1]
The best answer is B
Orange typically signalizes road work or construction.
4 0
4 years ago
Read 2 more answers
Discuss the what is software development​
aleksandr82 [10.1K]

Answer:

software development is the conceiveing, specifying , designing, programing , texting, document and fix involved in creating and application frame work or other software components.

5 0
3 years ago
Other questions:
  • Which form of Internet communication would a consumer seek out if they wanted to get personal perspectives into a company and it
    15·1 answer
  • Limitations of the information systems used by tesco​
    7·1 answer
  • Since cam systems regulate and self-mange the manufacturing process, you classify them as
    9·1 answer
  • A user creates a GPO. What do you need to do for that user to manage his or her GPO that he or she created?
    12·1 answer
  • Java Eclipse homework. I need help coding this
    6·1 answer
  • Is a ROUTER required for Internet Connectivity ?<br> Yes<br> No
    11·1 answer
  • The Next Einstein Initiative and the ________ Institute for Mathematical Sciences bring bright young learners and the best lectu
    15·1 answer
  • What is one example of an emerging class of software
    12·1 answer
  • Write a C program that reads two hexadecimal values from the keyboard and then stores the two values into two variables of type
    15·1 answer
  • Is social media bringing people together or cause in sepretation?​
    11·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!