Reverse words in a given String in C#


Let’s say the following is the string −

Welcome

After reversing the string, the words should be visible like −

emocleW

Use the reverse() method and try the following code to reverse words in a string −

Example

using System;
using System.Linq;

class Demo {

   static void Main() {
      string str = "Welcome";

      // reverse the string
      string res = string.Join(" ", str.Split(' ').Select(s => new String(s.Reverse().ToArray())));

      Console.WriteLine(res);
   }
}

Updated on: 21-Jun-2020

472 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements