Reverse word of full sentence

后端 未结 16 1883
眼角桃花
眼角桃花 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:36

        public static string reversewordsInsentence(string sentence)
        {
            string output = string.Empty;
            string word = string.Empty;
            foreach(char c in sentence)
            {
                if (c == ' ')
                {
                    output = word + ' ' + output;
                    word = string.Empty;
                }
                else
                {
                    word = word + c;
                }
            }
            output = word + ' ' + output;
            return output;
        }
    

提交回复
热议问题