Microsoft interview question

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

Interview Answer

Anonymous

12 Dec 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; }