hdu 1062 Text Reverse

Text Reverse

 

 思路:这道题就是一个字符串反转的问题,可以考虑使用栈的性质,先进后出,来完成这道题

代码:

#include<iostream>
#include<stack>
using namespace std;

int main(){
    int n;
    char ch;
    while (cin >> n){
        getchar();
        while (n--){
            stack<char> str;
            while (1){
                ch = getchar();
                if (ch == ' ' || ch == '\n' || ch == EOF){
                    while (!str.empty()){
                        cout << str.top();
                        str.pop();
                    }
                    if (ch == '\n' || ch == EOF){
                        break;
                    }
                    cout << " ";
                }else
                str.push(ch);
            }
            cout << endl;
        }

    }

    system("pause");
    return 0;
}

 

posted @ 2020-02-23 15:05  PCDL&TIPO  阅读(135)  评论(0编辑  收藏  举报