字符串反转方法总结
2017-07-27 15:22
246 查看
第一种:使用string.h中的strrev函数
[cpp] view
plain copy
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s[]="hello";
strrev(s);
cout<<s<<endl;
return 0;
}
第二种:使用algorithm中的reverse函数
[cpp] view
plain copy
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string s = "hello";
reverse(s.begin(),s.end());
cout<<s<<endl;
return 0;
}
第三种:自己编写
[cpp] view
plain copy
#include <iostream>
using namespace std;
void Reverse(char *s,int n){
for(int i=0,j=n-1;i<j;i++,j--){
char c=s[i];
s[i]=s[j];
s[j]=c;
}
}
int main()
{
char s[]="hello";
Reverse(s,5);
cout<<s<<endl;
return 0;
}
[cpp] view
plain copy
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s[]="hello";
strrev(s);
cout<<s<<endl;
return 0;
}
第二种:使用algorithm中的reverse函数
[cpp] view
plain copy
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string s = "hello";
reverse(s.begin(),s.end());
cout<<s<<endl;
return 0;
}
第三种:自己编写
[cpp] view
plain copy
#include <iostream>
using namespace std;
void Reverse(char *s,int n){
for(int i=0,j=n-1;i<j;i++,j--){
char c=s[i];
s[i]=s[j];
s[j]=c;
}
}
int main()
{
char s[]="hello";
Reverse(s,5);
cout<<s<<endl;
return 0;
}
相关文章推荐
- 【Java基础】总结java将字符串反转的7个方法
- 字符串反转方法总结
- 字符串反转方法总结
- 字符串反转-基于Python的6种方法比较(最全总结版本!)
- 【字符串反转总结】Java中七种方法实现
- 【字符串反转总结】Java中七种方法实现
- 实现字符串反转的三种方法
- javascript创建含数字字母的随机字符串方法总结
- java判断字符串是否为空的方法总结
- java判断字符串是否为空的方法总结
- js判断输入字符串是否为空、空格、null的方法总结
- hdu 1062Text Reverse 反转字符串【两种方法 数组法、堆栈】
- shell中截取字符串方法总结
- java 自定义的 字符串反转方法 和模拟除去 字符串两端空方法
- javaScript 截取字符串方法总结
- JavaScript实现反转字符串的方法详解
- C#操作字符串方法总结<转>
- 趣味算法:字符串反转的N种方法
- 总结:数据库连接字符串的使用方法
- JS字符串方法总结及字符串增删查改方法