1.使用string.h中的strrev函數
1 #include<stdio.h> 2 #include<string.h> 3 int main() 4 { 5 char s[]="hello"; 6 strrev(s); 7 puts(s); 8 return 0; 9 }
2.使用algorithm中的reverse函數
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 using namespace std; 5 int main() 6 { 7 string s= "hello"; 8 reverse(s.begin(),s.end()); 9 cout<<s<<endl; 10 return 0; 11 }
這兩個函數在我測試的時候出現了兩種完全不同的情況
1.strrev函數只對字符數組有效,對string類型是無效的。
2.reverse函數是反轉容器中的內容,對字符數組無效。