I want to print string in reverse format:
Input: My name is Archit Patel
Output: Patel Archit is name My
.
I\'ve tied the
public string GetReversedWords(string sentence)
{
try
{
var wordCollection = sentence.Trim().Split(' ');
StringBuilder builder = new StringBuilder();
foreach (var word in wordCollection)
{
var wordAsCharArray = word.ToCharArray();
Array.Reverse(wordAsCharArray);
builder.Append($"{new string(wordAsCharArray)} ");
}
return builder.ToString().Trim();
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}