字符串的单纯逆序输出(不需要保存)
2014-09-24 16:29 忘川沧海328 阅读(200) 评论(0) 收藏 举报#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
void ReversePrint(const char* s)
{
int len = strlen(s) ;
for (int i = len - 1; i >= 0; --i)
printf("%c",s[i]);
printf("\n");
}
int main(void)
{
char a[10] ="abcdefg";
char *q = NULL;
ReversePrint(a);
return 0;
}
方法2用指针移动来实现: 特别注意while结束时,p指向'\0',要做相应的处理
void ReversePrint(const char* s)
{
const char* p = s ;
while (*p)
p++ ;//此时p指向了字符串中的'\0';
--p ; //while结束时,p指向'\0',这里让p指向最后一个字符
while (p >= s)
{
printf("%c",*p);
--p ;
}
printf("\n");
}
浙公网安备 33010602011771号