c++中字符串反转的3种方法
2017-07-27 18:37
148 查看
第一种:使用algorithm中的reverse函数
第二种:自己编写
第三种:使用string.h中的strrev函数
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string s = "hello"; reverse(s.begin(),s.end()); cout<<s<<endl; return 0; }
第二种:自己编写
#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函数
#include <iostream> #include <cstring> using namespace std; int main() { char s[]="hello"; strrev(s); cout<<s<<endl; return 0; }
相关文章推荐
- c++中字符串反转的3种方法
- c++中字符串反转的3种方法
- c++中字符串反转的3种方法
- c++中字符串反转的3种方法
- c++中字符串反转的3种方法
- c++实用技巧:字符串反转的3种方法
- c++中字符串反转的3种方法<转>
- c++中字符串反转的3种方法
- c++ 字符串反转方法 UNICODE 和 ANSI 版本
- 在C++中可以用3种方法访问一个字符串
- C++字符串旋转3种方法实现
- C++常用字符串分割方法实例汇总
- C#反转字符串效率最高的方法
- 三种JAVA常用实现字符串反转(Reverse)的方法
- c++字符串连接方法大观
- C++输入一个字符串,把其中的字符按照逆序输出的两种方法
- C++输入不定长字符串方法
- C++字符串拷贝实现:递归方法&非递归方法
- 【C++】用C或C++实现void reverse(char* str)函数,即反转一个null结尾的字符串