1. 程式人生 > >輸入一個英文句子,翻轉句子中單詞的順序,但單詞內字元的順序不變。(筆試題) 句子中單詞以空格符隔開。為簡單起見,沒有標點符號。 例如輸入“I am a student”,則輸出“student a

輸入一個英文句子,翻轉句子中單詞的順序,但單詞內字元的順序不變。(筆試題) 句子中單詞以空格符隔開。為簡單起見,沒有標點符號。 例如輸入“I am a student”,則輸出“student a

輸入一個英文句子,翻轉句子中單詞的順序,但單詞內字元的順序不變。(筆試題)
句子中單詞以空格符隔開。為簡單起見,沒有標點符號。

例如輸入“I am a student”,則輸出“student a am I”

#include <stdio.h>
#include <string.h>


void Reverse(char * str, int beg, int end);
int main()
{   
char str[100];
gets(str);
    int i = 0, j = 0;
    for (; j <= (int)strlen(str); ++j) 
{
        if(' ' == str[j] || '\0' == str[j]) //zai kong ge huo /0 chu ji lu j
{
             Reverse(str, i, j - 1);
             i = j + 1;
        }//mei ge danci dao xu
    }
    Reverse(str, 0, strlen(str) - 1);//quan bu dao xu
    printf("%s\n", str);
    return 0;
}
void Reverse(char * str, int beg, int end)
{
int i;
    for(i = 0; i < (end - beg + 1) / 2; ++i) 
{
         char ch = str[beg + i];
         str[beg + i] = str[end - i];
         str[end - i] = ch;
    }
}

【result】: