字符串逆序(使用指针实现)
2014-09-24 16:03 忘川沧海328 阅读(1146) 评论(0) 收藏 举报- #include <stdio.h>
- #include <stdlib.h>
#include <malloc.h>
char* Reverse(char* s)
{
- //将q指向字符串最后一个字符
char* q = s ;
while( *q++ ) ;
q -= 2 ;
- //分配空间,存储逆序后的字符串。
- char* p = (char *)malloc(sizeof(char) * (q - s + 2)) ;
- char* r = p ; // 逆序存储
- while(q >= s)
- *p++ = *q-- ;
- *p = '\0' ;
- return r ;
- }
- int main(void)
- {
- char a[10] ="hello";
- char *q = NULL;
- q =Reverse(a);
- printf("%s\n",q);
- return 0;
- }
浙公网安备 33010602011771号