How To Reverse Each Word Of Given String

Introduction

In this article, we are going to explore how to reverse each word of the given string.

Example

Input: This is Kirtesh Shah

Result: sihT si hsetriK hahS

This is an essential technical interview question that may be posed to beginner, intermediate, and experienced candidates.

We previously covered how to reverse a string in the last article,

  1. How to Reverse Number in C#
  2. How to Reverse a String in C#
  3. Palindrome String Program in C#
  4. Palindrome Number in C#
  5. How to reverse order of the given string

Let’s start.

Reverse each word the give String

First create Asp.NET Core Console application and write the below code in the program.cs file.

Console.WriteLine("Enter a string"); 
var str = Console.ReadLine();
var strarray = str.Split(" "); 
List<string> lst = new();

foreach (var item in strarray)
{
    lst.Add(new string(item.Reverse().ToArray()));
}
var reverseOrder = String.Join(" ", lst);

Console.WriteLine($"{reverseOrder}");
Console.ReadLine();

Output

Reverse a string in C#

That’s all for this article. Hope you enjoy this article and learn something new.


Similar Articles