先看到的是一个递归输出的,不满足要求
void ret_str(char* s){ if(*s != '/0') { ret_str(s+1); } cout<<*s;}
找到的比较好的两种方法,没有static变量,线程安全
char* reverse(char* str){ int len = strlen(str); char tmp = str[len - 1]; if (len <= 1) return str; str[len - 1] = '/0'; reverse(str + 1); str[len - 1] = str[0]; str[0] = tmp; return str; } 下面两种算一种思想 void reverse(char *s) { if (*s) { char *p = s; char c = s[0]; do {*p = *(p+1); p++;}while(*p); reverse(s); s[strlen(s)] = c; } } char* reverse1(char *s) { if (*s) { int i; char c; i = strlen(s) - 1; c = s[i]; s[i] = 0; reverse1(s); for (i = strlen(s); i > 0 ; i--) s[i] = s[i-1]; s[0] = c; } return s; }
还有其他方法
void StrRev( char *str ) { if ( *(str) && *(str+1) ) { StrRev( str+1 ); while ( *(str+1) ) { *str ^= *(str+1); *(str+1) ^= *str; *(str) ^= *(str+1); str++; } } } char *reverse(char *str) { static int stk; static char *s; if(!stk) s = str; stk++; if(*str) { char *p = str, t = *p; while(p > s) { *p = *(p-1); p--; } *p = t; reverse(str+1); } stk--; return str; } void reverse(char *str) { char *tmp; int i; int len; char c; int add; len = strlen(str); if(len < 1) return; if(len&1) { add = len/2+1; } else { add = len/2; } tmp = str + add; reverse(tmp); for(i=0;i<len/2;i++) { c = str[i]; str[i] = str[i+add]; str[i+add] = c; } reverse(tmp); }