Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq Reverse string c#

Tags:

c#

linq

I want to reverse a string as given in the example.

 string[] actor = new string[] { "amitabh", "abhishek", "jitendra", "Salman", "Aishwariya" };

 var  resultstring= actor.Reverse().Select(c => c.Reverse()).ToArray();

    foreach(var m in resultstring)
    {
     //how to fetch this?
    }

the output should be

ayirawhsia,namlas,ardnetij,kehsihba,hbatima

Most of the post mentioned on how to reverse without using built in reverse().

I need the result using only reverse built in function using linq. I am able to make it as shown in the above snippet but couldnt reach the end.

Can someone help me to fix this?

like image 702
Tom Cruise Avatar asked Aug 11 '26 20:08

Tom Cruise


1 Answers

Change this line:

var resultstring = actor.Reverse().Select(c => c.Reverse()).ToArray();

To this:

var resultstring = actor.Reverse().Select(c => new string(c.Reverse().ToArray())).ToArray();

Of note here is the fact that when you called Reverse on the string it actually returned an IEnumerable<char> when you were probably expecting a string. No problem, the string constructor accepts an array of char which is what my tweak to your code does.

like image 173
test Avatar answered Aug 13 '26 10:08

test



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!