[Solved] Easy way to reverse a sentence using LINQ C#


 

This is the simple article gives you solution in one line code. 

Before you get into this article a short description of content gives a clarity.

C# LINQ String Handling


Introduction

If you are a developer, there could be multiple reasons you are here.

We can find anything in internet but not the way you want. 

For an example - Interview round

Imagine you have finished your technical round of interview, they asked you to write an algorithm to reverse all character in a sentence without using built in function.

So, when you back home trying to find a exact solution, think of that weird moment when you can not find a solution. Sometimes few website you may know they share technical knowledge of few. 

What are you trying to say here?

Exactly this is the same question raised as you did.

And I had few interview rounds asking me to write LINQ code, of a given problem.

However you prepared for an interview it is high chance that technical question sometimes weird. 

Let's explore more 

Can you explain the Problem?

Twisted question gives a head shak.....e

There are two tasks hidden in below question. It is string handling process of a sentence where in you may have to deal with word or char in sentence.

Question

Transform each word in the input text to lower case and rearrange all characters into alphabetical order.

What do you feel after reading above statement? 

How to identify problem?

Let's do paper work first by separating task;

You are given a sentence example "My Test for String PRocessors"

Task #1

From given sentence transform each word to lower case

Task #2:

Rearrange each word in alphabet order and return the result.

How to write code in C# LINQ?

From previous section we spilt tasks into two, and it is better to know what is input and what is the expected result.

Its is easy to write down input and output before coding.

INPUT:  My Test for String PRocesser
OUPUT:  ym tset rof gnirts  rossecorp 

This is the shortest code written in LINQ.

LINQ Code
string sentence = "My Test for String PRocesser";  
string.Join(" ", sentence .Reverse().Select(x => x.ToString().ToLower()));


Can you explain how above code works?

STEP 1:  String variable sentence is assigned with value "My Test for String PRocesser

STEP 2:  The second line sentence.Reverse() method reverses all chars in a word.

STEP 3:  After reversing select each word and if necessary as string use  x.ToString()

STEP 4:  The function ToLower() lowers all char in a lower cases

STEP 5:   string.Join(" ",  method joins all the reversed and lower case in string output


Summary

As to conclude, this article helps writing C# LINQ  code of string handling using inbuilt function.

The example and code written in this article are merely by practice in Visual Studio on my own, and I am happy to share my working solution and experience with you.

And I have provided only the code implementation and not the pre-requisites for a beginner.