Convert Integer to String

Code

#include <stdio.h>

void print_integer_as_string(int num) {
    // Your logic here
    char c;
    int i=0;
    int isnegative=0;
    char str[20] ={0};
    if(num==0)
    printf("%d",0);
    if(num <0)
    {
      isnegative =1;
      num = -1*num; //make it positive
    }
       
    while(num!=0)
    {  
        c = num%10;
        str[i++] = c + '0';
        num /= 10;
    }

    if(isnegative)
    str[i++] = '-';

     for (int j = i-1; j >= 0; j--)
     printf("%c ", str[j]);
}

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