HDU 1062

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40924940/article/details/83545368

一道简单的字符处理水题,题意 输入n 之后跟随 n个英文句子,把每个句子里的单词(当然就是空格隔开的)翻转过来(包括标点符号)。

用栈可以轻松处理。。AC代码如下,先想再看。。

扫描二维码关注公众号,回复: 3951779 查看本文章
#include<cstdio>
#include<stack>
using namespace std;
int main()
{
    int n;
    char ch;
	scanf("%d",&n);
	getchar(); 
	while(n--)
	{
		stack<char> s; 
		while(true)
		{
	        ch=getchar(); 
            if(ch==' '||ch=='\n')
			{
				while(s.size())
				{
					printf("%c",s.top()); 
					s.pop(); 
				}
				if(ch=='\n'||ch==EOF)  
					break; 
				printf(" ");
			}
			else
			    s.push(ch);
		}
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40924940/article/details/83545368