1.對於用char定義的字符串:使用string.h中的strrev函數
#include <iostream> #include <cstring> using namespace std; int main() { char s[]="123456";//不能是string類型; strrev(s); cout<<s<<endl; return 0; }
2.對於string類型的:使用algorithm中的reverse函數
#include<iostream> #include <cstring> #include <algorithm> using namespace std; int main() { string s[]="123456"; reverse(s.begin(),s.end()); cout<<s<<endl; return 0; }
3.自己編寫函數:對於字符串的兩邊進行交換。