Question d’entretien chez Zynga

Convert a string to an integer

Réponses aux questions d'entretien

Utilisateur anonyme

10 sept. 2009

Not exactly super, but should do for a whiteboard: int myatoi (char *str) { int retval; int sign; sign = 1; if (*str == '-') { sign = -1; str++; } retval = 0; while (*str) { retval *= 10; retval += *str - '0'; str++; } return (retval * sign); }

2

Utilisateur anonyme

6 août 2012

The pointer is a char pointer, so the loop (and the sign check) are on a character by character basis.

Utilisateur anonyme

10 nov. 2013

Brian is right, he is using C char pointers, and can compare a character at a time. This is not Java, this does work.

Utilisateur anonyme

3 juil. 2015

int.Parse(str) hah. But in all seriousness, the aforementioned solution is good.

Utilisateur anonyme

7 juil. 2010

Bug, The check is just for the first character of the string - your version will not even compile...

Utilisateur anonyme

7 juil. 2010

I think, there is bug at statement if (*str == '-1') ........ what if str = "-1234"