Reverse the ordering of words in a string

后端 未结 30 4186
青春惊慌失措
青春惊慌失措 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:12

    Here is a C implementation that is doing the word reversing inlace, and it has O(n) complexity.

    char* reverse(char *str, char wordend=0)
    {
        char c;
        size_t len = 0;
        if (wordend==0) {
            len = strlen(str);
        }
        else {
            for(size_t i=0;str[i]!=wordend && str[i]!=0;i++)
                len = i+1;
        }
                for(size_t i=0;i

提交回复
热议问题