A.单词反转
Time Limit: 1000 MS | Memory Limit: 65536 KB |
Total Submissions: 669 | Accepted: 326 |
Description
给你一些英文句子,请将这些句子中的每个英语单词反转,然后再将其输出。这里所说的英语单词仅由大、小写英文字母组成。
Input
多个英文句子,每句占一行,且每句不超过80个字符。
Output
按题目要求输出。
Sample Input
Hello world!
Happy programming, happy life!
Happy programming, happy life!
Sample Output
olleH dlrow!
yppaH gnimmargorp, yppah efil!
yppaH gnimmargorp, yppah efil!
解题思路:利用栈简单模拟 即可
Source
2013 Anhui College Student Programming Contest--ahfywff
#include<iostream>
#include<string>
#include<stack>
#include<cstring>
using namespace std;
string str;
int main() {
while(getline(cin,str)) {
int len=str.length();
stack<char> s;
for(int i=0; i< len ; i++) {
if(isalpha(str[i])) {
s.push(str[i]);
} else {
while(!s.empty()) {
cout<<s.top();
s.pop();
}
cout<<str[i];
}
}
cout<<endl;
}
return 0;
}