Страницы

Translate

четверг, 29 августа 2013 г.

Exercise 4.12. Adapt the ideas of printd to write a recursive version of itoa

Exercise 4.12. Adapt the ideas of printd to write a recursive version of itoa; that is, convert
an integer into a string by calling a recursive routine.


#include <stdio.h>

#define LEN 100

void itoa(int n, char s[]);

int abs(int x);

int main()
{
    int n;
    char s[LEN];
    
    printf("Enter the number: ");
    scanf("%d", &n);
    itoa(n, s);
    printf("%s\n", s);
    return 0;
}

/*itoa*/
void itoa(int n, char s[])
{
    static int i; 
    int sign;
    
    i = 0;
    sign = n;
    if(n/10)
        itoa(n/10, s);
    else
    {

        if(sign < 0)
            s[i++] = '-';
    }
    s[i++] = abs(n % 10) + '0';
    s[i] = '\0';
}

Комментариев нет:

Отправить комментарий