C++ 编写一个void reverse(string &s)的函数,使字符串倒序输出

如题所述

第1个回答  2012-04-25
#include <string>
#include <algorithm>

void reverse(string &s) {
std::reverse(s.begin(), s.end());
}本回答被网友采纳
第2个回答  2012-04-12
#include <string>
#include <algorithm>
int main(){
std::string s = "1234567890";
std::reverse(s.begin(), s.end());
}
第3个回答  2012-04-11
#include <iostream>
#include <string>
using namespace std;
void reverse( string& s)
{
int start=0,end=s.length();
string tem(s);
while(start<end)
{
end--;swap(tem[start],tem[end]);start++;
}
s=tem;
cout<<s;
}
int main()
{
string str;
cout<<"please input a string:";
getline(cin,str);
reverse(str);
cout<<endl;
return 0;
}
第4个回答  2014-01-17
1 #include<stdio.h> 2 int dlsplay1(char *string); 3 int dlsplay2(char *string); 4 int main() 5 { 6 char sting[]="embedded linux"; 7 dlsplay1(string); 8 dlsplay2(string); 9 } 10 int dlsplay1(char *string) 11 { 12 printf("the original string is %s /n",string); 13 } 14 int dlsplay2(char *string1) 15 { 16 char *string2; 17 int size,i; 18 size=strlen(string1); 19 string2=(char *)molloc(size +1); 20 for(i=0;i<size;i++) 21 string2[size-1-1]=string1[i]; 22 string2[size+1]=''; 23 printf("the string afterward is %s/n",string2); 24 } 25 }
相似回答