DomainFlag DomainFlag - 3 months ago 18
C Question

Recursive way of display alphabet in c

I am quite newbie in c, so I just starting off with some code, experimenting some stuff, right now I am stuck with this problem in C, creating a function that displays the alphabet in lowercase, on a single line, by ascending order, starting from the letter ’a’.

This is where I am stuck:

#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;
}

Answer

Here, you won't print anything really interesting. In fact, your application will crash because printf() require at least a char * parameter (a string).

Your alfabet() function seems not so bad, but you should print the letter in it :

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);
}
Comments