EN
C# / .NET - reverse words in a given string
0 points
In this article, we would like to show you how to reverse words in a given string in C#.
In the example below, we use the Array.Reverse()
method, which argument is a string converted into an array of words.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
static string ReverseWords(string originalString)
6
{
7
string[] arrayOfWords = originalString.Split(" ");
8
Array.Reverse( arrayOfWords );
9
return String.Join(" ", arrayOfWords);
10
}
11
12
public static void Main(string[] args)
13
{
14
string originalString = "This is your text...";
15
16
string reversedWordsString = ReverseWords(originalString);
17
Console.WriteLine(reversedWordsString); // text... your is This
18
}
19
}
Output:
xxxxxxxxxx
1
text... your is This