Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Reverse Words in a String

XPChallenge Points: 20
levelLevel: Medium

You are given a string s consisting of words separated by spaces.
Your task is to reverse the order of the words in the string.

A word is defined as a sequence of non-space characters.
The words in s are separated by one or more spaces.

Return a new string containing the words in reverse order,
joined by a single space, with no leading or trailing spaces.

Example 1:

Input: s = " keep pushing forward "

Output: "forward pushing keep"

Example 2:

Input: s = "learn code every day"

Output: "day every code learn"

Example 3:

Input: s = "success requires consistency"

Output: "consistency requires success"

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result