Today while solving a problem, I had to convert integers to string in C to use its indexing capabilities. One simple solution was to use sprintf, which can be done in 2-3 lines, but i was temped to solve this problem without any string manipulation functions.
In the above program we used malloc (for dynamic memory allocation) and pow. I had to look at an ASCII table for reference on how to convert from int to ASCII and rest all is pure logic.
Compiling :
we used the -lm flag for math libraries
Output:
The Code
Code: c
// Change a positive integer to a string in C
#include <stdio.h>
#include <stdlib.h> // for malloc
#include <math.h> // for pow
int get_number_of_digits(int n)
{
int i, count;
for (i = 10, count = 0; ; i *= 10, count++) {
if ((int)n / i == 0) {
break;
}
}
return count + 1;
}
int main()
{
int number, no_of_digits, prev = 0, i, j;
char *number_string;
scanf("%d", &number);
no_of_digits = get_number_of_digits(number);
number_string = (char *)malloc(sizeof(char) * (no_of_digits + 1)); // +1 for NULL
for (i = 0, j = pow(10, no_of_digits - 1); i < no_of_digits; i++, j /= 10) {
number_string[i] = (number / j - (prev * 10)) + 48; // int to ascii
prev = number / j;
}
number_string[i] = '\0';
printf("%s\n", number_string);
}
Compiling :
Code:
gcc -lm int_to_str.c -o int_to_str
Output:
Code:
lionaneesh@lionaneesh:~/Challenges/$ ./int_to_str 123 123
Very good tut, althouhg I miss some more explanation about the code...
