一、strrev( ) 函数
头文件:#include<cstring> 或 #include<string.h>
#include<iostream>
#include<cstring>
using namespace std;
#define N 20
int main(){
char ch[N]="hello!";
strrev(ch);
cout<<ch;
return 0;
}
如果是 使用string类定义的字符串对象,则不能使用strrev()函数,因为它的参数要求是:char *类型
二、reverse()函数
头文件:#include <algorithm> 因为要用到strlen()函数计算字符串ch的长度,所以还得加头文件:#include <cstring> 或者 #include<string.h>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define N 20
int main(){
char ch[N]="hello!";
reverse(ch,ch+strlen(ch));
cout<<ch;
return 0;
}
如果是 使用string类定义的字符串对象,也能使用reverse()函数
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define N 20
int main(){
string str="hello!";
reverse(str.begin(),str.end());
cout<<str;
return 0;
}
以上程序运行的结果均为:
!olleh
—<完>—