一、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
—<完>—