Monday, April 4, 2016

ReversingStringsUsingCSharp

Our Logic in CSharp (C#):

string input = "DS tech blog";

string result = string.Join(" ", input
                      .Split(' ')
                      .Select(x => new String(x.Reverse().ToArray())));

Console.WriteLine(result);

Remember to include the below using statements:

using System;
using System.Linq; 


How-to-reverse-each-word-in-a-string-using-c#

Explanation for the above code:

Split the input string using a single space as the separator.
  • Split() method for returning a string array that contains each word of the input string.
  • Select method for constructing a new string array, by reversing each character in each word.
  • Join method for converting the string array into a string.

No comments:

HTMLCode

HTMLCode Content