All submissions

Convert Integer to String

 

#include <stdio.h>

void print_integer_as_string(int num) {
    // Your logic here
    if (num == 0) 
        printf("0");

    char buffer[10];  
    int i = 0, is_negative = 0;

    if (num < 0) 
    {
        is_negative = 1;
        num = -num;
    }

    // Convert digits in reverse
    while (num > 0) 
    {
        buffer[i++] = (num % 10) + '0';
        num /= 10;
    }

    if (is_negative) 
        buffer[i++] = '-';
    

    // Print characters in reverse order with space
    for (int j = i - 1; j >= 0; j--) 
    {
        printf("%c", buffer[j]);
        if (j > 0) printf(" ");
    }
}

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

 

 

 

 

Loading...

Input

123

Expected Output

1 2 3