这是天津大学2015考研的编程题
Problem Description
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.
Input
The
input contains several test cases. The first line of the input is a
single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.
Output
For each test case, you should output the text which is processed.
Sample Input
3
olleh !dlrow
m‘I morf .udh
I ekil .mca
Sample Output
hello world!
I‘m from hdu.
I like acm.
Hint
Remember to use getchar() to read ‘\n‘ after the interger T, then you may use gets() to read a line and process it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char c,buffer[1001];
char* text_end;//每段字符串的结束地址
char* text_begin;//每段字符串的开始地址
int i=0,j=0;
int cnt;
char temp;
scanf("%d",&cnt);
while(getchar()!=‘\n‘);//输入case数目的时候错了,你只读最后一个字符。。13你会读成3。。
while(i<cnt)
{
gets(buffer);
strcat(buffer," \0");//为了方便输出,将输入的字符添加空格和字符串结束符
text_begin=&buffer[0];//初始化第一段字符串的起始地址
for(j=0; j<strlen(buffer); j++)
{
if(buffer[j]==‘ ‘)//遇到空格时,将字符串翻转
{
text_end=&buffer[j-1];//字符串的结束地址
while(text_begin<text_end)//字符串翻转
{
temp=*text_begin;
*text_begin=*text_end;
*text_end=temp;
text_begin++;
if(text_begin==text_end) break;
text_end--;
}
text_begin=&buffer[j+1];//每段字符串结束重置开始地址
}
}
buffer[strlen(buffer)-1]=‘\0‘;//删除结尾字符串的空格字符
printf("%s\n",buffer);
i++;
}
return 0;
}