All submissions

Convert Integer to String

Code

#include <stdio.h>

void print_integer_as_string(int num) {
    int neg = 0,cnt = 0,start = 0,value = num,rem;
    if(num<0){
        neg  = 1;
        value = -num;
    }
    if(num==0){
        cnt = 1;
        printf("0");
        return ;

    }
    char arr[12] = {0};
    while(value != 0){
        rem = value % 10;
        arr[start] = rem + 48;
        value = value/10;
        start++; 
        cnt++;     
    }
    if(neg){
        arr[start] = '-';
        cnt++;
    }
    for (int j=cnt-1;j>=0;j--){
        printf("%c ",arr[j]);
    }
    
    // printf("%s",arr);

}

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

Solving Approach

 

 

 

Loading...

Input

123

Expected Output

1 2 3