Easy way to reverse each word in a sentence

后端 未结 9 2328
深忆病人
深忆病人 2021-01-02 12:59

Example:

string str = \"I am going to reverse myself.\";
string strrev = \"I ma gniog ot esrever .flesym\"; //An easy way to achieve this

A

9条回答
  •  我在风中等你
    2021-01-02 13:31

    1 - Extension method to reverse a string

      public static string reverseString(this string description) {
    
            char[] array = description.ToCharArray().Reverse().ToArray();
    
            return new string(array);
        }
    

    2 - Reversing an array and reversing all string of this array

        public static string reverseText(this string description) {
    
          string [] reversetext=  description.Split(' ').Select(i => i.ToString().reverseString()).Reverse().ToArray();
    
          return string.Join(" ", reversetext);
        }
    

提交回复
热议问题