1 //反转字符串及数字 2 3 #include<iostream> 4 #include<string> 5 using namespace std; 6 7 /*int reverseString(char *s) { 8 //int i=0,j=i+strlen(s)-1; 9 int len=strlen(s); 10 char *begin,*end; 11 begin=s; 12 end=begin+len-1; 13 if(s!=NULL){ 14 15 while(begin<end) 16 { 17 char temp; 18 temp=*begin; 19 *begin=*end; 20 *end=temp; 21 begin++; 22 end--; 23 // swap(s[begin++],s[end--]); //c++翻转 24 } 25 } 26 //return s; 27 } 28 */ 29 string reverseString(string s) 30 { 31 int i=0,j=s.size()-1; 32 while(i<j) 33 { 34 /*char t=s[i]; 35 s[i++]=s[j]; 36 s[j--]=t;*/ 37 swap(s[i++],s[j--]);//反转 38 } 39 return s; 40 } 41 int main() 42 { 43 char a[1000]; 44 gets(a); 45 string ss=reverseString(a); 46 cout<<ss; 47 //puts(a); 48 return 0; 49 }