HelloWorld开发者社区

www.helloworld.net - 开发者专属的技术社区

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
 1 //递归反向打印字符串
 2 void reversePrint(const char *str)
 3 {
 4     if(str == NULL)
 5         return;
 6     if(*str == '\0')
 7         return;    
 8     reversePrint(str+1);//递归下一个字符
 9     printf("%c",*str);    //打印当前字符
10 }

下面是非递归打印

//非递归反向打印字符串
void nonReversePrint(const char *str)
{
    if(str == NULL)
        return;
    
    //获取字符串的长度
    int index = strlen(str) - 1;
    while(index >= 0)
    {
        printf("%c",str[index]);
        index--;
    }
}

 

posted on 2016-01-22 12:00  HelloWorld开发者社区  阅读(261)  评论(0编辑  收藏  举报