How to reverse each word in a string with C#

2 Answers

0 votes
using System;
  
class ReverseEachWordInAString_CSharp
{
    static string ReverseEachWordInAString(string s) {
        string [] arr = s.Split(' ');
   
        for (int i = 0; i < arr.Length; i++) {
            char[] charArray = arr[i].ToCharArray();
            Array.Reverse(charArray);
            arr[i] = new string(charArray);
        }
 
        return string.Join(" ", arr);
    }
    static void Main() {
        string s = "C# is a general purpose programming language";
  
        s = ReverseEachWordInAString(s);
              
        Console.Write(s);
    }
}


   
/*
run:
   
#C si a lareneg esoprup gnimmargorp egaugnal
   
*/

 



answered Sep 4, 2021 by avibootz
edited Aug 30, 2024 by avibootz
0 votes
using System;
using System.Linq;

class ReverseEachWordInAString_CSharp
{
    static void Main() {
        string s = "C# is a general purpose programming language";
  
        s = string.Join(" ", s.Split(' ').Select(x => new String(x.Reverse().ToArray())));  
   
        Console.WriteLine(s);   
    }
}


   
/*
run:
   
#C si a lareneg esoprup gnimmargorp egaugnal
   
*/

 



answered Sep 4, 2021 by avibootz
edited Aug 30, 2024 by avibootz

Related questions

1 answer 88 views
1 answer 90 views
1 answer 114 views
1 answer 124 views
124 views asked Aug 31, 2024 by avibootz
...