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
Define the term algorithm and describe how programmers use algorithms when designing a program.
Ber [7]

Answer:

programming algorithm as a recipe that describes the exact steps needed for the computer to solve a problem or reach a goal. In many cases, there are multiple ways to perform a specific operation within a software program. Therefore, programmers usually seek to create the most efficient algorithms possible. By using highly efficient algorithms, developers can ensure their programs run as fast as possible and use minimal system resources. Of course, not all algorithms are created perfectly the first time. Therefore, developers often improve existing algorithms and include them in future software updates. When you see a new version of a software program that has been "optimized" or has "faster performance," it most means the new version includes more efficient algorithms.

Explanation:

3 0
3 years ago
I’ll give Brainly if u answer all please <br> ASAP
Yuki888 [10]

Answer:

Answers are: 38, 135, 23, 209, 53 & 181 respectively

Explanation:

8 0
3 years ago
You can access decorative underlines and change their color through the ____ drop-down.
Dominik [7]
The answer is "underline gallery or underline drop-down". The user can access the decorative underlines and change the underline's color through the underline gallery. Underlines can be added for decoration purposes, to make your work look formal or creative.
4 0
3 years ago
Write a program consisting of: a. A function named right Triangle() that accepts the lengths of two sides of a right triangle as
Lunna [17]

Answer:

The java program is as follows.

import java.lang.*;

public class Triangle

{

   //variables to hold sides of a triangle

   static double height;

   static double base;

   static double hypo;

   //method to compute hypotenuse

   static double rightTriangle(double h, double b)

   {

       return Math.sqrt(h*h + b*b);

   }

public static void main(String[] args) {

    height = 4;

    base = 3;

    hypo = rightTriangle(height, base);

 System.out.printf("The hypotenuse of the right-angled triangle is %.4f", hypo);

}

}

OUTPUT

The hypotenuse of the right-angled triangle is 5.0000

Explanation:

1. The variables to hold all the three sides of a triangle are declared as double. The variables are declared at class level and hence, declared with keyword static.

2. The method, rightTriangle() takes the height and base of a triangle and computes and returns the value of the hypotenuse. The square root of the sum of both the sides is obtained using Math.sqrt() method.

3. The method, rightTriangle(), is also declared static since it is called inside the main() method which is a static method.

4. Inside main(), the method, rightTriangle() is called and takes the height and base variables are parameters. These variables are initialized inside main().

5. The value returned by the method, rightTriangle(), is assigned to the variable, hypo.

6. The value of the hypotenuse of the triangle which is stored in the variable, hypo, is displayed to the user.

7. The value of the hypotenuse is displayed with 4 decimal places which is done using printf() method and %.4f format specifier. The number 4 can be changed to any number, depending upon the decimal places required.

8. In java, all the code is written inside a class.

9. The name of the program is same as the name of the class having the main() method.

10. The class having the main() method is declared public.

11. All the variables declared outside main() and inside another method, are local to that particular method. While the variables declared outside main() and inside class are always declared static in java.

4 0
3 years ago
In each iteration, selection sort (small) places which element in the correct location?
Jet001 [13]
The answer is <span>smallest not yet placed.  </span><span>In each iteration, selection sort  places smallest not yet placed element in the correct location.  </span>
7 0
3 years ago
Other questions:
  • Which of the following rules should be used to keep the appropriate distance between your vehicle and the vehicle in front of yo
    10·2 answers
  • Which network protocol is used to route IP addresses?<br>A. TCP<br>B. UDP<br>C. IP<br>D. ICMP
    15·1 answer
  • Which column and row references are updated when you copy the formula: =F$5+12?
    14·1 answer
  • A SmartArt graphic consists of two parts: the SmartArt graphic itself and a(n) ____ pane.
    5·1 answer
  • A student is curious about how a Web site appears on his computer screen. On a piece of paper,
    8·1 answer
  • Which of the following are common data types? Check all that apply.
    6·1 answer
  • Type a statement using srand() to seed random number generation using variable seedVal. Then type two statements using rand() to
    12·1 answer
  • Write a program that requests the user input a word, then prints every other letter of the word starting with the first letter.
    5·1 answer
  • Which century saw the development of letterpress printing?
    10·1 answer
  • True or false: If you have a 32-bit CPU architecture, it's recommended to use a 64-bit operating system.
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!