One common C# interview question is, How would you reverse the words in a string? This is interesting because it combines several methods in C# and .NET. Some code is much better than other code, and you want the finest version.
Usually the most clever and obscure method is not preferred. My approach here was to develop a simple, reliable, and efficient method. Your team members will be glad to maintain this code.
Here is the code. The important steps are numbered in the methods. First I cover the Main method, and then the ReverseWords code.
using System;
class Program
{
static void Main()
{
//
// 1.
// Declare two const strings to reverse words in.
// Const improves performance and clarity.
//
const string sentenceA = "Bill Gates is the richest man on Earth";
const string sentenceB = "Srinivasa Ramanujan was a brilliant mathematician";
//
// 2.
// Call static method and print results.
//
string reverseA = ReverseWords(sentenceA);
Console.WriteLine(reverseA);
//
// 3.
// Repeat for string B.
//
string reverseB = ReverseWords(sentenceB);
Console.WriteLine(reverseB);
}
/// <summary>
/// Receive string of words and return them in the reversed order.
/// </summary>
/// <param name="sentence">String you want to reverse.</param>
/// <returns>Reversed words.</returns>
static string ReverseWords(string sentence)
{
//
// 1.
// Separate words by spaces.
//
string[] words = sentence.Split(' ');
//
// 2.
// Use static Reverse method provided by the framework.
//
Array.Reverse(words);
//
// 3.
// Use static string Join method and return value.
//
return string.Join(" ", words);
}
}Main method steps. The main method first declares the sentences you want to reverse. 1. I use the 'const' modifier here, which means the strings can't be changed. 2. First sentence is reversed. 3. Second sentence is reversed. Console.WriteLine prints to the console.
ReverseWords method steps. First, this method is static because it doesn't save state. 1. Separate words on spaces with Split. 2. Reverse the words with the efficient Array.Reverse method. 3. Join the array on a space.
The two sentences will be printed in reverse order to the console window in Visual Studio. To run the above program, paste the entire code sample into a new Console Application project.
Earth on man richest the is Gates Bill mathematician brilliant a was Ramanujan Srinivasa
This is not a clever or unique solution. However, it is clear and uses few lines of code. It is easy to enhance when your requirements change. Other sites show more complex solutions or faster solutions.
Add XML comments to methods by typing three slashes above a method ///. XML comments are important because Visual Studio reads them and provides useful tips from them.
Prefer simple and straightforward solutions to this sort of problem. They likely perform very well and are simple to maintain. Elaborate and clever methods just lead to more bugs and typos.
Similar articles. Sometimes you need to reverse the individual characters in a string. My article Reverse String has useful resources on that problem.