Pergunta de entrevista da empresa Microsoft

Implement a function that turns a string into a number with sign.

Resposta da entrevista

Sigiloso

12 de dez. de 2012

Here's a C# solution: int strToInt( string str){ int i = 1, num = 0, len = str.Length(); bool isNeg = false; if( str[0] == '-') { isNeg = true; } while( i < len){ num*=10 num+= (str[i++] - '0'); } if ( isNeg ) { num *= -1; } return num; }