我寫了兩種實現方法,其中第二種是參考Yomman園友的(http://www.cnblogs.com/yomman/p/4271949.html)。我的方法(方法一)是用一個數組存放輸入的字符串,另一個數組存放字符串中每個字的首地址,然後······;方法二是利用OJ會自動在輸入結尾添加文件結束符,我是沒想到,而且在自己的編譯器(Dev-C++)上實現不了,但確實是簡潔了不少,還有點小毛病,有點浪費空間。題設要求及代碼實現如下,其中方法一被注釋
/*
Name:
Copyright:
Author:
Date: 03/04/15 07:59
Description:
給定一句英語,要求你編寫程序,將句中所有單詞的順序顛倒輸出。
輸入格式:測試輸入包含一個測試用例,在一行內給出總長度不超過80的字符串。字符串由若干單詞和若干空格組成,其中單詞是由英文字母(大小寫有區分)組成的字符串,單詞之間用1個空格分開,輸入保證句子末尾沒有多余的空格。
輸出格式:每個測試用例的輸出占一行,輸出倒序後的句子。
輸入樣例:
Hello World Here I Come
輸出樣例:
Come I Here World Hello
*/
#include <stdio.h>
//#include <string.h>
//#include <stdbool.h>
#define MAX 80
//refering to another's
int main()
{
// freopen("in.txt", "r", stdin); // for test
char s[MAX / 2][MAX + 1];
int cnt;
cnt = 0;
while(scanf("%s", s[cnt++]) != EOF);
cnt -= 2;
while(cnt)
printf("%s ", s[cnt--]);
printf("%s\n", s[cnt]);
// fclose(stdin); // for test
return 0;
}
/*
void inverse(char * s, int l);
int main()
{
// freopen("in.txt", "r", stdin); // for test
char s[MAX + 1];
int l;
gets(s);
l = strlen(s);
inverse(s, l);
// fclose(stdin); // for test
return 0;
}
void inverse(char * s, int l)
{
char index[l / 2 + 1];
int i, cnt, tmp;
bool head;
cnt = 0;
head = true;
for(i = 0; i < l; i++)
{
if(s[i] != ' ')
{
if(head)
{
index[cnt++] = i;
head = false;
}
}
else
head = true;
}
do
{
tmp = index[--cnt];
while(s[tmp] != ' ' && s[tmp])
putchar(s[tmp++]);
if(cnt)
printf(" ");
}while(cnt);
printf("\n");
}
*/