How to reverse the words and the order of the words in a string with C#

1 Answer

0 votes
using System;
 
class Program
{
    static string ReverseWords(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);
        }
        
        Array.Reverse(arr);
         
        return string.Join(" ", arr);
    }
    static void Main() {
            string s = "C# is a general purpose programming language";
 
            s = ReverseWords(s);
             
            Console.Write(s);
    }
}
 
 
 
 
/*
run:
            
egaugnal gnimmargorp esoprup lareneg a si #C
            
*/

 



answered Sep 4, 2021 by avibootz
...