Reverse each word in a string using c#


reverse, each, word, string, using, c#, linq, dot net, .net, sentence,

In this video we will discuss how to reverse each word in a string using c#. This is a common c# interview question.

Reverse the following string
one two three four five

Output should be
eno owt eerht ruof evif

Here is the C# code that can do this
string inputString = “one two three four five”;
string resultString = string.Join(” “, inputString
.Split(‘ ‘)
.Select(x =] new String(x.Reverse().ToArray())));


Make sure you have the following using declarations
using System;
using System.Linq;

Here is what is happening with the above code
Split the input string using a single space as the separator. Split() method returns a string array that contains each word of the input string.
Select method, constructs a new string array, by reversing each character in each word.
Join method converts the string array into a string.

Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video


Xem Thêm Bài Viết Khác:


  1. Alternative method:
    string inputString = "One Two Three Four Five";

    var InPutArray= inputString.ToCharArray();


    var joinedString = string.Join("", InPutArray);

  2. Sir please explain how to reverse a string without reversing words, that means instead of eno owt eerht i want three two one to be displayed

  3. Thank you for your videos sir, i got job becuase of your video before 5 years. Thank you. am telling my friends to watch your videoS :). tHANKS AGAiN:). hOPE YOU WLL REPLY.

  4. Sir if my ID is like N001,N002… and I want to print all ID once but per ID per page how to loop or what is the easy method in c# using visual studio

  5. ERROR :

    Severity Code Description Project File Line Suppression State
    Error CS1503 Argument 2: cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'string[]'

  6. can we use this code it is very simple also…. please describe difference between both the codes

    static void Main(string[] args)
    Console.WriteLine("Enter a sentence");
    string s = Console.ReadLine();
    string[] a = s.Split(' ');
    Console.WriteLine("after reverse your sentence you get:");
    for(int i=0;i<=a.Length-1;i++)
    Console.Write(a[i] + "" + ' ');

  7. i was given a project about the human resource management. there are many employees in an IT company. one of them is a programmer whose have attributes :: programming language and hava a behaviour of code, fix, and bugs. now i don't understand how to write a program for this.can you show me a sample solution for this one plzzzzzzz.

  8. how to write a program that counts the number of string. the sentecne ends with dot(.). or ! mark or a question mark
    somebody write this progarm

  9. Sir, What is the use of Static Class?? Plz.. reply this Question and also make a video for this question………This question ask me on interview…

  10. Consider a string like * string word = " Company is Great" * Make a program that will convert the string array to be like
    Great is Company ……Please Make a Video on this…


Please enter your comment!
Please enter your name here