Reverse words in a given String in C#

CsharpProgrammingServer Side Programming

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);
   }
}
raja
Published on 14-Aug-2018 09:24:25
Advertisements