Write a program to convert integer to string (character array) without library function.

#include<stdio.h>

char String[100];

int IntToString(int n) {
    int k = 0;
    if(n == 0) {
    return 0;
    }
    k = IntToString(n/10);
    String[k] = n%10 +'0';
    return k+1;
}

void main() {
    int n, k = 0;
    clrscr();
    printf("\n Please Enter Integer Number : - ");
    scanf("%d",&n);
    if(n == 0) {
    String[0] = 0+'0';
    String[1] = NULL;
    }
    else {
    k = IntToString(n);
    String[k] = NULL;
    }
    printf("\n\n Integer Number Converted into String is : - ");
    printf("%s\n",String);
    getch();
}

0 comments:

Post a Comment