C# program to Reverse words in a string


Let’s say the following is the string −

Hello World

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

olleH dlroW

Example

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

Live Demo

using System;
using System.Linq;
class Demo {
   static void Main() {
      // original string
      string str = "Hello World";
      // reverse the string
      string res = string.Join(" ", str.Split(' ').Select(s => new String(s.Reverse().ToArray())));
      Console.WriteLine(res);
   }
}

Output

olleH dlroW

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 19-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements