TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Reverse Words

This C# program shows how to reverse the words in a sentence. It uses the Array.Reverse method.

Reverse words. Words in a string can be reversed.

This might answer a common interview question. We combine several methods and concepts from the .NET runtime, resulting in a straightforward method.

Example. First, the most clever and obscure method is often not preferred. My approach here was to develop a simple and reliable static method. Your team members will be glad to maintain this code.

Static Method

C# program that reverses sentences

using System;

static class WordTools
{
    /// <summary>
    /// Receive string of words and return them in the reversed order.
    /// </summary>
    public static string ReverseWords(string sentence)
    {
	string[] words = sentence.Split(' ');
	Array.Reverse(words);
	return string.Join(" ", words);
    }
}

class Program
{
    static void Main()
    {
	const string s1 = "Bill Gates is the richest man on Earth";
	const string s2 = "Srinivasa Ramanujan was a brilliant mathematician";

	string rev1 = WordTools.ReverseWords(s1);
	Console.WriteLine(rev1);

	string rev2 = WordTools.ReverseWords(s2);
	Console.WriteLine(rev2);
    }
}

Output

Earth on man richest the is Gates Bill
mathematician brilliant a was Ramanujan Srinivasa

Main method steps. The main method first declares the sentences you want to reverse. I use the const modifier here, which means the strings can't be changed. The first sentence is reversed, and then the second sentence is reversed.

Const

Next: The Console.WriteLine static method prints the sentence results to the console.

Console.WriteLine

 

ReverseWords method steps. First, this method is static because it doesn't save state. Separate words on spaces with Split. Reverse the words with the efficient Array.Reverse method. Join the array on a space.

Array.Reverse

Summary. We reversed words in a string using a method that 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.

Tip: Many teams prefer simple and straightforward solutions to this sort of problem. They are easy to maintain.

Warning: Elaborate and clever methods, with clever syntax or optimizations, often just lead to more bugs and typos.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf