Microsoft

  www.microsoft.com
  www.microsoft.com

Interview Question

Software Development Engineer (SDE) Interview

Write a function that takes a string as an input and

  outputs an integer, e.g. turning "1234" into 1234.
Tags:
ots
Answer

Interview Answer

3 Answers

1

I used -- int i = Convert.ToInt32(a);

Interview Candidate on Mar 7, 2014
2

public static long atoi(String input)
    {
        long number = 0;
        for (int i = 0; i < input.length(); i++)
        {
            number = number * 10 + Character.getNumericValue(input.charAt(i));
        }

        return number;
    }

NP on Mar 8, 2014
5

public static in strToInt(String str){
int i=0; num=0;
boolean isNeg=false;
int len=str.lenght();
if(str.charAt(0)=='-'){
isNeg=true;
i=1;
}
while(i<len){
num*=10;
num+=(str.charAt(i++)-'0');
}
if(isNeg){
num=-num;
return num;
}

AA on Mar 9, 2014

Add Answers or Comments

To comment on this, Sign In or Sign Up.