Employeur impliqué
How would you implement int atoi ( const char * str );
Utilisateur anonyme
I parsed the string from left to right, multiplying the temp int by 10 for each char traversed.
You need to subtract the ascii value of zero to get the integer value of the char before multiplying by 10.
#include #include unsigned int my_atoi(const char* str) { int num = 0; while (*str) { num *= 10; num += (*str++ - '0'); } return num; } int main(int argc, char* argv[]) { printf("atoi(%s)=%d\n",argv[1], my_atoi(argv[1]) ); return 0; }
Tenez-vous au courant des dernières opportunités et profitez de conseils d’initiés en suivant les entreprises de vos rêves.
Obtenez des recommandations et des mises à jour personnalisées en démarrant vos recherches.