Add C# reverse words in a string
parent
d3c2184af8
commit
d1f7426a61
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
|
||||
//Reverse the order of the words in a given string
|
||||
//Example: "Hello World" becomes "World Hello"
|
||||
|
||||
namespace Algorithms.Strings
|
||||
{
|
||||
public class ReverseWordsInString
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
Console.WriteLine("Please enter a string");
|
||||
string originalString = Console.ReadLine();
|
||||
|
||||
Console.WriteLine(ReverseWords(originalString));
|
||||
}
|
||||
|
||||
public static string ReverseWords(string input)
|
||||
{
|
||||
string[] words = input.Split(' ');
|
||||
|
||||
Array.Reverse(words);
|
||||
|
||||
string reversedString = string.Join(" ", words);
|
||||
|
||||
return reversedString;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue