Easy way to reverse each word in a sentence

后端 未结 9 2215
深忆病人
深忆病人 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);
        }
    
    0 讨论(0)
  • 2021-01-02 13:31
    string str = "I am going to reverse myself.";
    
    string[] strArray = str.Split(' ');
    
    string[] strArrayReversed = new string[strArray.Length];
    
    for(int i = 0; i < strArray.Length; i++) 
    {
    
       char[] eachWordLetter = strArray[i].ToCharArray();
    
       Array.Reverse(eachWordLetter);
    
       strArrayReversed[i] = new string(eachWordLetter);
    }
    
    string newReversedString = String.Join(" ", strArrayReversed);
    
    Console.WriteLine(newReversedString);
    Console.ReadLine();
    
    0 讨论(0)
  • 2021-01-02 13:31
    public static void ReverseEachWordString(string abc)
            {
                int start_index = 0, end_index = abc.Length - 1;
                int i = 0, j = 0;
                char[] arr = abc.ToCharArray();
                try
                {
                    while (start_index < end_index)
                    {
                        if (arr[start_index] == ' ')
                        {
                            Console.WriteLine(arr[start_index]);
                            start_index++;
                            i = start_index;
                        }
                        else
                        {
                            if (arr[i] != ' ')
                            {
                                if (i == end_index)
                                {
                                    i++;
                                    for (j = i - 1; j >= start_index; j--)
                                    {
                                        Console.WriteLine(arr[j]);
                                    }
                                    break;
                                }
                                else
                                    i++;
                            }
                            else
                            {
                                for (j = i - 1; j >= start_index; j--)
                                {
                                    Console.WriteLine(arr[j]);
                                }
                                i++;
                                start_index = i - 1;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    ex.Message.ToString();
                }
            }       
    
    0 讨论(0)
提交回复
热议问题