Reverse word of full sentence

后端 未结 16 1889
眼角桃花
眼角桃花 2020-12-06 20:04

I want to print string in reverse format:

Input: My name is Archit Patel

Output: Patel Archit is name My.

I\'ve tied the

16条回答
  •  自闭症患者
    2020-12-06 20:50

    Here is an easy way

            public static string MethodExercise14Logic(string str)
            {
                string[] sentenceWords = str.Split(' ');
                Array.Reverse(sentenceWords);
                string newSentence = string.Join(" ", sentenceWords);
                return newSentence;
            }

    1. You first create an array called sentence word and use the split method to populate the array with all your words. Note that space is used as a delimiter, telling the split method when exactly to split.
    2. You then use the Reverse method in the Array class to reverse the complete array.
    3. You then use the Join method of the string class to join your now reversed array into a string called newSentence.
    4. Finally the method then returns the newSentence string.

提交回复
热议问题