Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program that 1. Reads in a line of text 2. Then outputs the line so that the first word and the second word

Write a program that 1. Reads in a line of text 2. Then outputs the line so that

the first word and the second word are swapped

No space at the beginning of the line

Assumptions about the line of text:

There may be some spaces before the first word.

The line contains no punctuation, words are separated from each other by one or more spaces

Remark: using methods of string, see textbook, or the API online

Sample Output: please enter a line of text with no punctuation one two three Four five six seven I have changed your text: two one three Four five six seven

Hints: The sentence can be viewed as

spaces followed by firstWord followed by secondWord followed by otherWords

use trim method to remove spaces before the firstWord

use indexOf to find the first space position in the line

use substring to get first word and the rest of the line

use substring to find the second word, which is the first word of the rest of the line

form a new string use concatenation

secondWord firstWord otherWord

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 31st International Conference Dexa 2020 Bratislava Slovakia September 14 17 2020 Proceedings Part 1 Lncs 12391

Authors: Sven Hartmann ,Josef Kung ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

303059002X, 978-3030590024

More Books

Students also viewed these Databases questions