c++实用技巧:字符串反转的3种方法
2018-02-28 16:45
204 查看
原文:http://blog.csdn.net/szu_aker/article/details/52422191
第一种:使用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;
}
第一种:使用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;
}
相关文章推荐
- c++中字符串反转的3种方法
- c++中字符串反转的3种方法<转>
- c++中字符串反转的3种方法
- c++中字符串反转的3种方法
- c++中字符串反转的3种方法
- c++中字符串反转的3种方法
- c++中字符串反转的3种方法
- c++中字符串反转的3种方法
- ASP.NET技巧:两个截取字符串的实用方法
- 在C++中可以用3种方法访问一个字符串
- ASP.NET两个截取字符串的实用方法技巧
- C++字符串旋转3种方法实现
- c++ 字符串反转方法 UNICODE 和 ANSI 版本
- ASP.NET技巧:两个截取字符串的实用方法
- Shell中实现字符串反转方法分享
- 一种巧妙的反转字符串的方法及思考过程
- 7 种将字符串反转的 Java 方法
- hdu 2072 1106学一波字符串分割,C语言与C++两种方法
- Visual C++.NET中 字符串转换方法[2]