Reverse the ordering of words in a string

后端 未结 30 3778
青春惊慌失措
青春惊慌失措 2020-11-22 10:23

I have this string s1 = \"My name is X Y Z\" and I want to reverse the order of the words so that s1 = \"Z Y X is name My\".

I can do it u

30条回答
  •  北海茫月
    2020-11-22 11:13

    c# solution to reverse words in a sentence

    using System;
    class helloworld {
        public void ReverseString(String[] words) {
            int end = words.Length-1;
            for (int start = 0; start < end; start++) {
                String tempc;
                if (start < end ) {
                    tempc = words[start];
                    words[start] = words[end];
                    words[end--] = tempc;
                }
            }
            foreach (String s1 in words) {
                Console.Write("{0} ",s1);
            }
        }
    }
    class reverse {
        static void Main() {
            string s= "beauty lies in the heart of the peaople";
            String[] sent_char=s.Split(' ');
            helloworld h1 = new helloworld();
            h1.ReverseString(sent_char);
        }
    }
    

    output: peaople the of heart the in lies beauty Press any key to continue . . .

提交回复
热议问题