TheDeveloperBlog.com

Home | Contact Us

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

<< Back to C-SHARP

C# Reverse Words

Reverse the words in a sentence with the Split, Join, and Array.Reverse methods.
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.Reverse String
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

Main: We see the sentences we want to reverse. We use the const modifier here, which means the strings can't be changed.

const

ReverseWords: First this method is static because it doesn't save state. It separates words on spaces with Split.

Split

Then: ReverseWords reverses the words with the efficient Array.Reverse method. It then joins the array on a space.

Array.ReverseJoin

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

Console
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
Notes, solutions. Many teams prefer simple and straightforward solutions to this sort of problem. They are easy to maintain. Elaborate and clever methods, with clever syntax or optimizations, often just lead to more bugs.
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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


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