Languages
[Edit]
EN

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

0 points
Created by:
Dirask Admin - Marwin
1460

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

C# / .NET - String (popular problems)

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