From d1f7426a61312ee63e6235594c871eaac9e7e516 Mon Sep 17 00:00:00 2001 From: katkat825 <137170306+katkat825@users.noreply.github.com> Date: Fri, 30 Jun 2023 12:42:16 -0400 Subject: [PATCH] Add C# reverse words in a string --- .../src/Strings/reverse-words-in-string.cs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 algorithms/CSharp/src/Strings/reverse-words-in-string.cs diff --git a/algorithms/CSharp/src/Strings/reverse-words-in-string.cs b/algorithms/CSharp/src/Strings/reverse-words-in-string.cs new file mode 100644 index 00000000..2372ce30 --- /dev/null +++ b/algorithms/CSharp/src/Strings/reverse-words-in-string.cs @@ -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; + } + } +} +