题目描述:
- 将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I”
- 所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符
- 数据范围:输入的字符串长度满足1≤n≤1000
- 注意本题有多组输入
- 输入描述:输入一个英文语句,每个单词用空格隔开。保证输入只包含空格和字母。
- 输出描述:得到逆序的句子
示例:
输入:I am a boy
输出:boy a am I
输入:nowcoder
输出:nowcoder
思路:
- 直接翻转一下
- 把每个单词找出来,原地自身翻转
具体实现:
void reverse(char *pstart,char *pend)
{
char tmp
while(pstart<pend)
{
tmp=*pstart
*pstart=*pend
*pend=tmp
pstart++
pend--
}
}
void word_reverse()
{
char c[1000]
char *ps,*pe
while(gets(c)!=NULL)
{
reverse(c,c+strlen(c)-1)
ps=pe=c
while(*ps)
{
while(*ps!=' '&&*ps)
ps++
reverse(pe,ps-1)
while(*ps==' ')
ps++
pe=ps
}
puts(c)
}
}
int main()
{
word_reverse()
system("pause")
}