首页 > 学院 > 开发设计 > 正文

华为OJ:翻转字符串

2019-11-08 02:16:01
字体:
来源:转载
供稿:网友

题目描述 写出一个程序,接受一个字符串,然后输出该字符串反转后的字符串。例如:

输入描述: 输入N个字符

输出描述: 输出该字符串反转后的字符串

输入例子: abcd

输出例子: dcba

解答1:#include <iostream>#include <stack>#include <string>using namespace std;int main(){ string str; int i; stack<char>s; while(getline(cin,str)) { for(i=0;i<str.length();++i) { s.push(str[i]); } while(!s.empty()) { cout<<s.top(); s.pop(); } cout<<endl; } return 0;}解答2:#include <iostream>#include <string>using namespace std;int main(){ string str; cin >> str; for(int i = str.size()-1; i >=0;i--) cout << str[i];}解答3:#include<iostream>#include<string>#include<algorithm>using namespace std;int main(){ string str; while (cin >> str) { reverse(str.begin(), str.end()); cout << str<<endl; }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表