普通字符串
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
string s;
while(cin>>s){
for(int i=s.size()-1;i>=0;i--)
{
cout<<s[i];
}
cout<<endl;
}
}
含有空格的字符串:
可以利用栈来实现:
/*获得一行文本,用栈倒序输出这行文本*/
#include <iostream>
#include <stack>//使用标准库里面的栈
#include <cstring>
#include <string>
using namespace std;
int main()
{
string temp;
getline(cin,temp);//获取一行可能包含有空格的文本
int len = temp.size();
const char *ss = temp.c_str();//将string转换成char*
stack <char> text;
int i = 0;
while(len--) {//入栈
text.push(ss[i++]);
}
while(!text.empty()){//出栈
cout << text.top();
text.pop();
}
return 0;
}