Text Reverse -文本反转(Stack栈)

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.

伊格内修斯喜欢以相反的方式写字。给定由 Ignatius 编写的一行文本,您应该反转所有单词,然后输出它们。

输入

输入包含几个测试用例。输入的第一行是一个整数 T,它是测试用例的数量。T测试用例如下。
每个测试用例包含一行,其中包含多个单词。一行最多有 1000 个字符。

输出

对于每个测试用例,您应该输出处理的文本。

Sample Input

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

Sample Output

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

暗示

记得使用getchar() 读取整数T 后的'\n',然后您可以使用gets() 读取一行并处理它。

题意描述:

对于输入的测试案例,将里面所以单词倒转然后输出

解题思路:

利用栈后进先出,将开始的单词存入栈中,直到遇到第一个空格,将前面的单词输出,对于最后一个单词,最后一个字母后面是换行 所以要单独处理最后一个字母

注意: 注意最后一个单词的处理

AC

#include<stdio.h>
#include<string.h>
#include<stack>
#include<iostream>
using namespace std;
int main()
{
	int n;
	cin>>n;
	getchar();//获取\n 
	while(n--)
	{
		
		char a[1010];
		gets(a);
		stack<char>s;
		for(int i=0;i<strlen(a);i++)
		{
			if(a[i]==' ')//遇到空格,将栈内的单词放出 
			{
				while(!s.empty())
				{
					cout<<s.top();
					s.pop();
				}
				cout<<" ";
			}
			else if(a[i+1]=='\0') //检测最后一个单词 以及最后一个字母 
			{
				s.push(a[i]);
				while(!s.empty())
				{
					cout<<s.top();
					s.pop();
				}
			}
			else
			{
				s.push(a[i]);//将原单词一个一个进栈 
			}
		}
		printf("\n");
	} 
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值