字符串倒序输出 2011.04.07

来源:互联网 发布:数据库程序设计教程 编辑:程序博客网 时间:2024/05/18 02:26

#include <iostream>

using namespace std;

 

int main()

{

char a[] = "hello,world!";

for (int i=strlen(a)-1; i>=0; i--)

{

cout<<a[i];

}

cout<<endl;

return 0;

}

输出:!dlrow,olleh

在字符数组里面,其实是利用的指针遍历。

 

#include <iostream>

using namespace std;

#include <string>

#include <algorithm>

 

int main()

{

string str = "hello,world";

reverse(str.begin(), str.end());

cout<<str<<endl;

return 0;

}

输出:!dlrow,olleh

 

还是用stl简单啊,用一个reverse的倒序算法就直接解决了

 

原创粉丝点击