输入一个字符串,把其中的字符按逆序输出。如输入LIGHT,输出THGIL。要求用string方法。

输入LIGHT,输出THGIL只是举一个例子,我需要的程序是输入任意字符串的

你好,以下使用了string类以及反向枚举器实现逆序输出:
#include <string>
#include <iostream>
using namespace std;

void main()
{

string str;
cout<<"请输入字符串:";
cin>>str;
// 获得反向的枚举器,反向遍历
string::reverse_iterator iter = str.rbegin();
cout<<"输出字符串:"
while(iter!=str.rend())
{
cout<<*iter;
iter++;
}

getchar();
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-10-13
#include <stdio.h>
#include<string>
int main( void )
{
char s[81];
scanf("%s",s);
printf("%s\n",strrev(s));
return 0;
}
//vs2005调试通过
第2个回答  2010-10-13
string方法 -- C++

#include<iostream>
#include<string>
using namespace std;
int main()
{
string x;
int L;
cout<<"Please enter a string:"<<endl;
cin >> x;
L = x.length();
while ( L > 0){
L--;
cout << x.at(L);
}
return 0;
}
第3个回答  2010-10-13
string reverse(const string& s)
{
int start=0;
int end=s.length();
string temp(s);

while(start< end){
swap(temp[start],temp[end]);
start++;
end--;
}
return temp;
}

void swap(char& v1,char& v2)
{
char temp=v1;
v1=v2;
v2=temp;
}