现在的位置: 首页 > 综合 > 正文

输入一个字符串,将其逆序后输出

2013年08月07日 ⁄ 综合 ⁄ 共 807字 ⁄ 字号 评论关闭

 #include <iostream>
#include <string>
#include <algorithm>
//字符串倒置
using namespace std;

void main(){
 /*

///////////////////////////////////////////////////////////////////////////////////////////////
 //方法1
 char a[50];
 cin.getline(a,50,'\n');
 int i;
 int j=strlen(a)-1;
 for (i=0;i<strlen(a)/2;i++,j--)
 {
  char temp;
  temp=a[i];
  a[i]=a[j];
  a[j]=temp;
 }
 for (i=0;i<strlen(a);i++)
  cout<<a[i];

///////////////////////////////////////////////////////////////////////////////////////////////

  */

 

 

///////////////////////////////////////////////////////////////////////////////////////////////

 //方法2
 string str;
 cin>>str;
 //str.assign(str.rbegin(),str.rend());   //这两个函数都可以实现
    reverse(str.begin(),str.end());
 cout<<str;

///////////////////////////////////////////////////////////////////////////////////////////////

 system("pause");
}

抱歉!评论已关闭.