字符串反转方法总结
2017-07-27 15:22
197 查看
第一种:使用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中七种方法实现
- 【Java基础】总结java将字符串反转的7个方法
- 字符串反转-基于Python的6种方法比较(最全总结版本!)
- 字符串反转的七种方法(java)
- 几种字符串反转方法效率比较
- JS字符串方法总结
- javascript 实现字符串反转的三种方法
- [总结]关于去除字符串中重复出现长度变化的注释文字的方法
- shell中截取字符串方法总结
- javascript中的数组和字符串方法总结
- linux中shell截取字符串方法总结
- Python实现字符串反转的几种方法
- Shell中实现字符串反转方法
- java判断字符串是否为空的方法总结
- 字符串、数组、JSON字符串、JSON对象、DOMList转换方法总结
- 定义一个静态成员方法,该方法实现字符串反转
- php反转中英文字符串的方法