输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。(笔试题) 句子中单词以空格符隔开。为简单起见,没有标点符号。 例如输入“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】: