哈理工OJ-2330-Final Ugly English【栈】

本文介绍了一个简单的字符串处理问题,即如何将输入的一行由小写字母和空格组成的文本中的每个单词进行逆序输出。通过使用栈的数据结构,可以有效地解决这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

哈理工OJ-2330
Description
ACM twist-toy encountered such a problem in the work, this article, to ensure that this article only lowercase letters
and spaces, please send the articles in each word inverted output, such as “hello world this is an competition”. You
should output “olleh dlrow siht si na noititepmoc”.
Input
A group of data, each line of data input from the lower case letters and spaces of the article, the length of not more than
one thousand
Output
Output a line for each word after the reversal of the article.
Sample Input
hello world this is an competition
Sample Output
olleh dlrow siht si na noititepmoc

题意:
逆序输出字母,看样例
解析:
栈解决一下

#include<bits/stdc++.h>
using namespace std;
int main()
{
    char a[1111];
    while(gets(a))
    {
        stack<char>sta;
        int len=strlen(a);

        for(int i=0; i<=len; i++)
        {
            if(a[i]==' '||i==len)
            {

                while(!sta.empty())
                {
                    printf("%c",sta.top());
                    sta.pop();
                }

                if(i!=len)
                    printf(" ");
            }
            else
            {
                sta.push(a[i]);
            }


        }
         printf("\n");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值