How we can Reverse Each Word of String

Let us consider we have to reverse the following string: "I am reversing". After implementing the following code, we can expect the output as "I ma esrever" .

Code Snippet to reverse

  1. string endv = "";  
  2. string word = "";  
  3. string s = "I am reversing";  
  4. foreach(char ch in s)   
  5. {  
  6.     if (ch == ' ')   
  7.     {  
  8.         endv += new string(word.Reverse().ToArray());  
  9.         endv += " ";  
  10.         word = "";  
  11.     }   
  12.     else   
  13.     {  
  14.         word += ch;  
  15.     }  
  16. }  
  17. endv += " ";  
  18. endv += new string(word.Reverse().ToArray());  
  19. Console.WriteLine(endv);  
  20. Console.ReadKey();