Can anyone help me write a C function, itoa (which logically complements the atoi function)? Takes an integer and an empty char array, converts the integer into a string of ASCII characters which represent the integer, and stores it into the empty char array.
#include <stdio.h> #include <stdlib.h> int mkDigit( int number, int base, char* answer ) { char characters[] = { "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" }; int remainder, index; if( number == 0 ) return( 0 ); remainder = number % base; number /= base; index = mkDigit( number, base, answer ); answer[ index ] = characters[ remainder ]; return( ++index ); } int main( int argc, char *argv[] ) { char answer[19]; int number = atoi( argv[1] ); mkDigit( number, 10, answer ); puts( answer ); return( 0 ); }
Join our real-time social learning platform and learn together with your friends!