So here we have a function that requires both the integer holding the number and the string that the number is to be inserted into as parameters. It will return 1 if it was successful and 0 if it wasn't.
int my_itoa(char* str, int num) {Because I didn't want the function to require any libraries, all it can really check for is if the char* pointer is actually pointing to something.
int i, digit, neg = 0, length = 1, x = 0;
if(str != NULL) {
if(num < 0) {
neg = num;
num = -num;
}
for(i = num / 10; i > 0 ; i /= 10) {
length++;
}
for(i = length - 1; i >= 0; i--) {
if(neg < 0) {
str[0] = '-';
neg = 1;
}
digit = num % 10;
str[i + neg] = digit + '0';
num /= 10;
}
str[length + neg] = '\0';
x = 1;
}
return x;
}
No comments:
Post a Comment