Convert Integer to String

Code

#include <stdio.h>

void print_integer_as_string(int num) {
    // Your logic here
    char c[12];
    int i = 0;

    if(num == 0){ /////zero case
        printf("0 ");
        return;
    }

    if(num<0){ /////handle negative nums
        putchar('-');
        putchar(' ');
        num = -num;
    }

    while(num > 0){ /////prepare strings from num
        c[i++] = (num % 10) + '0';
        num/=10;
    }

    for(int j = i-1; j>= 0; j--){ /////printing without printf
        putchar(c[j]);
        putchar(' ');
    }


}

int main() {
    int num;
    scanf("%d", &num);
    print_integer_as_string(num);
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

123

Expected Output

1 2 3