ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

ReverseWords

Reverse Words

Source

public static string ReverseWords(this string sentence)
{
    var words = sentence.Split(' ');
    Array.Reverse(words);
    //string reversed = words.Aggregate((workingSentence, next) => next + " " + workingSentence);

    return string.Join(" ", words);
}

Example

var sentence = "the quick brown fox jumps over the lazy dog";
sentence.ReverseWords();

Author: Ali HajiAbadi

Submitted on: 22 jan. 2017

Language: C#

Type: string

Views: 3782