Drill

ProblemsC# › warmup

Reverse the order of the words

easywarmupStringsParsingC#

A text filter turns a sentence back to front, word by word, without disturbing the words themselves.

ReverseWords(text: string) → string

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public string ReverseWords(string text) {
    
}

Worked examples

CallResult
ReverseWords("the quick brown fox")"fox brown quick the"
ReverseWords(" a b ")"b a"
ReverseWords("one")"one"
ReverseWords("")""

Hint

Split on whitespace, reverse the list, join with a single space.

Reference solution in C#
public string ReverseWords(string text) {
    var words = text.Split((char[]) null, StringSplitOptions.RemoveEmptyEntries).ToList();
    words.Reverse();
    return string.Join(" ", words);
}

The same problem in another language

More warmup problems in C#