Add C# reverse words in a string

pull/1215/head
katkat825 2023-06-30 12:42:16 -04:00
parent d3c2184af8
commit d1f7426a61
1 changed files with 30 additions and 0 deletions

View File

@ -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;
}
}
}