再坚持一下—————第九天

XUPT_ACM的泰泰学长智商180,他有个怪癖——写字的时候喜欢倒着写。泰泰学长的女朋友收到泰泰学长的信可发了愁,你能帮泰泰学长的女朋友翻译吗? 

Input

输入包含多组数据。 第一行输入一个正整数 T 表示总的行数。接下来是T行。 
每行包含一些单词,每行最多1000个字符。 

Output

对于每一行输入,请你都把他倒过来,泰泰学长的女朋友会谢谢你的哟。 

Sample Input

3
olleh !dlrow
m'I morf .udh
I ekil .mca

Sample Output

hello world!
I'm from hdu.
I like acm.


  QAQ思路正确,可是输出格式不正确。
#include<stdio.h>
#include<string.h>
int main()
{
	int n;
	char str[1001];
	int len,i,j,flag,h,q;
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		gets(str);
		len=strlen(str);
		str[len]='8';//防满足i+1; 
		flag=0;
	    h=q=0;
		for(i=0;i<len;i++)
		{
			if(str[i+1]==' ')
			{
				flag=1;        //说明字符串中含有空格 
				h=i;
				//倒叙输出
				for(j=h;j>=q;j--){ 
				printf("%c",str[j]);
				} 
				printf(" ");
				q=h+2;
			}
		}
		if(flag==0){               //字符串中没有空格 
			for(i=0;i<len;i++)
			printf("%c",str[i]);
			printf(" \n");
		}
		else{                     //含有空格 
			for(j=(len-1);j>=q;j--)
			printf("%c",str[j]);
			printf(" \n");
		}
	}
	
	
return 0;	
}

Hint

在读入整数T之后不要忘了用 getchar() 来读入 '\n' , 你可能会用到 gets() 来读入一行。

思路如下:

问题的关键就是找到空格的前一个位置

首先分为两种情况

扫描二维码关注公众号,回复: 5097521 查看本文章

1.字符串中没有空格,那么直接倒序输出就行了

2.字符串中有空格,那么用q和h分别指向字符串中的每一小字符串的头和尾,倒序输出就行啦。

猜你喜欢

转载自blog.csdn.net/weixin_42143003/article/details/86631973
今日推荐