answer.
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]
1 year 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]1 year 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
Which best compares and contrasts visual and performing arts? Both careers use communication skills; however, people involved in
Vlad1618 [11]

Answer:

Both careers speak to an audience; however, people involved in the performing arts speak only to a live audience.

Explanation:

sorry im late but im not sure

3 0
2 years ago
Read 2 more answers
Respond to the following in a paragraph of no less than 125 words. Describe the steps to active listening.
Agata [3.3K]

Answer

• Maintaining eye contact

• Paying attention in the talk

• Listening without jumping to conclusion

• Formulating a picture in mind as you listen

• Avoid interruption to suggest solutions

• Asking clarification when the speaker has paused

• Asking questions to improve understanding

• Understanding the mood of the speaker

• Giving the appropriate feedback

Explanation

A person can improve his active listening skills by being aware of his or her personal style of communicating. A good listener is productive  and able to influence others when talking because he or she will have mastered the technique of persuading and negotiating with audience when talking. The journey to becoming an active listener is through paying attention, showing that you are following the speaker ,providing good feedback, avoiding judging the speaker and providing the appropriate feedback.



8 0
2 years ago
Read 2 more answers
What is the output of 1101 x 10 == 11000 + 10?
Marta_Voda [28]
== is an operator that returns a boolean if both operand are equal.

1101 * 10 is 11010

11000 + 10 is 11010


11010 == 11010

Thus the output would be True.
5 0
1 year ago
Which is among the earliest formats of audio used in video games? A. MP3 B. wave table synthesis C. pulse code modulation D. MOD
solong [7]

Answer:

MOD

Explanation:

MOD audio file format is primarily used to represent music. It uses the .MOD file extension and has had popularity as background music for independent video games. It would be safe to say that MOD audio file extensions are the most widespread trackers that were used and are still being used in many computer games and demos.

0 0
1 year ago
Read 2 more answers
C# Write, compile, and test a program named PersonalInfo that displays a person’s name, birthdate, work phone number, and cell p
Soloha48 [4]

Answer:

#include <stdio.h>

#Include<iostream>

using namespace std;

//Define required function

PersonalInfo( )   {

    printf("Name   : Robert Josh\n");  

    printf("DOB    : July 14, 1975\n");  

    printf("Work Phone : 00-00000000\n");  

    printf("Cell Phone : 11-777777777\n");  

    return(0);  

 }

int main()   {

    //Call function

 PersonalInfo( );

 return 0;

 }

Explanation:

Its just a simple code to print required details when the function PersonalInfo is called.

5 0
2 years ago
Other questions:
  • Column, bar, pie, line, and scatter are all types of_____
    9·1 answer
  • In the game Beehive, you play the role of a worker bee who must watch over her hive. Your duties include (among others) directin
    12·2 answers
  • Identify the normalized form of the mantissa in 111.01.
    14·1 answer
  • Write the printItem() method for the base class. Sample output for below program:
    8·1 answer
  • Define a method named roleOf that takes the name of an actor as an argument and returns that actor's role. If the actor is not i
    8·1 answer
  • Your employer, yPlum Corporation is manufacturing two types of products: Mirabelle smartphone, and Blackamber laptop. The compan
    14·1 answer
  • 1.1.5 practice: analyzing business culture
    13·1 answer
  • In this code, identify the repeated pattern and replace it with a function called month_days, that receives the name of the mont
    14·1 answer
  • 12. Write C statement(s) that accomplish the following. a. Declare int variables x and y. Initialize x to 25 and y to 18. b. Dec
    12·1 answer
  • A(n) ________ server tracks who is logging on to the network as well as which services on the network are available to each user
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!