在c中显示字母表的递归方式

Dom*_*lag 0 c

我是c的新手,所以我只是从一些代码开始,尝试一些东西,现在我在C中遇到这个问题,创建一个以小写形式显示字母表的函数,在一行上,按升序排列,从字母'a'开始.

这是我被困的地方:

#include <stdio.h>

int alfabet(unsigned int i) {    
   if(i <= 122) {
       char litera = i;
       return litera;
   }
   return alfabet(i+1);
}

int main() {
    int i = 97;
    printf(alfabet(i));
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

nac*_*yde 6

在这里,你不会打印任何真正有趣的东西.实际上,您的应用程序将崩溃,因为printf()至少需要一个char *参数(一个字符串).

你的alfabet()功能似乎没那么糟糕,但是你应该在里面打印这封信:

int alfabet(unsigned int i) 
{

    if (i > 'z') {
        // Here is the stop condition. 
        // If the value is higher than 122 ('z' character), we stop recursivity)
        return; 
    }

    printf("%c ", i);

    // Otherwise, let's call this function with another character
    return alfabet(i+1);
}
Run Code Online (Sandbox Code Playgroud)

  • 可能用'z'代替122 (2认同)