All submissions

Convert Integer to String

Code

#include <stdio.h>
#include <stdint.h>

void print_integer_as_string(int num) {
    if (!num) {
        printf("0");
        return;
    }

    char num_str[12] = {0};
    char *str = num_str;

    uint8_t is_negative = (num < 0); 

    if (is_negative) {
        num = -num;
    }

    while (num) {
        *str = (num % 10) + '0';
        num /= 10;
        ++str;
    }
    --str;

    if (is_negative) {
        printf("- ");
    }

    while (*str != '\0') {
        printf("%c ", *str);
        --str;
    }

}

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

Solving Approach

 

 

 

Loading...

Input

123

Expected Output

1 2 3