Languages
[Edit]
EN

C# / .NET - reverse words in a given string

0 points
Created by:
Barmar
338

In this article, we would like to show you how to reverse words in a given string in C#.

Practical example

In the example below, we use the Array.Reverse() method, which argument is a string converted into an array of words.

using System;

public class Program
{
    static string ReverseWords(string originalString)
    {
        string[] arrayOfWords = originalString.Split(" ");
        Array.Reverse( arrayOfWords );
        return String.Join(" ", arrayOfWords);
    }

    public static void Main(string[] args)
    {
        string originalString = "This is your text...";

        string reversedWordsString = ReverseWords(originalString);
        Console.WriteLine(reversedWordsString); // text... your is This
    }
}

Output:

text... your is This
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

C# / .NET - String (popular problems)

C# / .NET - reverse words in a given string
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join