## Zillow Interview Question

Software Engineer / Developers**Country:**United States

```
public static long StringToLong(String s) throws NumberFormatException{
long n = 0;
int factor = 1;
int c;
for(int i = s.length()-1; i > 0; --i ){
c = s.charAt(i);
if(c < 48 || c > 57){
throw new NumberFormatException(s+" is not a valid number.");
}
n += (c-48) * factor;
factor *= 10;
}
c = s.charAt(0);
if(c == '-'){
return -n;
}else{
n += (c-48) * factor;
}
return n;
}
```

```
long StringToLong(String str)
{
/* code goes here to convert a string to a long */
long result = 0;
int factor = 1;
for(int i=str.length()-1;i>-1;i--)
{
char c = str.charAt(i);
int k = c-48;
result = (factor*k)+result;
factor = factor * 10;
}
System.out.println("Therefore the long number is : " + result);
return result;
}
```

public class stringtolong {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

long i = stringToLong("123");

if (i == 123)

System.out.println("success !!");

else

System.out.println("failure !!");

}

static long stringToLong(String s){

long value = 0, longNumber=1;

char counter; //counter to read each character in string

int i=s.length()-1;

while(i>=0)

{

counter = s.charAt(i);

value = value + (counter-48) * longNumber;

longNumber = longNumber * 10;

i--;

}

return value; //returns long value of the given string

}

}

Extarct all the characters from start and add it in long.

logic:

char[] lookupNumber = {'1','2','3'....'8','9'};

string c = "1000";

long l;

while(c!=empty)

{

extractedCharacter = extartFromStartOfString(string c);

l = l*10 + lookup(extractCharacter);

}

int lookup(char c)

{

return binarySearchInLookupNumber (C)

//this would return index.

}

This uses no inbuilt function of any language.

int strlen(const std::string a){

int length=0;

while(a[length])

length++;

//throw exception if string is empty

if(length<1) throw -1;

return length;

}

//look up the ascii code

//and check if any non-numerical char exist

long lookup(char a){

int temp=int(a);

if(temp<48 || temp>57) throw temp;

switch(temp){

case 48:

return 0;

case 49:

return 1;

case 50:

return 2;

case 51:

return 3;

case 52:

return 4;

case 53:

return 5;

case 54:

return 6;

case 55:

return 7;

case 56:

return 8;

case 57:

return 9;

default:

return -1;

:#include "atol.h"

//compute string length

int strlen(const std::string a){

int length=0;

while(a[length])

length++;

//throw exception if string is empty

if(length<1) throw -1;

return length;

}

//look up the ascii code

//and check if any non-numerical char exist

long lookup(char a){

int temp=int(a);

if(temp<48 || temp>57) throw temp;

switch(temp){

case 48:

return 0;

case 49:

return 1;

case 50:

return 2;

case 51:

return 3;

case 52:

return 4;

case 53:

return 5;

case 54:

return 6;

case 55:

return 7;

case 56:

return 8;

case 57:

return 9;

default:

return -1;

}

}

//convert string to long

long stringToLong(const std::string s){

int len=strlen(s);

long temp=0;

long temp2=0;

//flag for negative input

int flag=1;

int i=0;

//check negativity

if(s[0]=='-'){

flag=-1;

i++;

}

for(;i<len;i++)

{

//throw an exception if the number is too large (or too small)

if((LONG_MAX-10*temp)<lookup(s[i])) throw 99;

temp=10*temp+lookup(s[i]);

}

return flag*temp;

}

private long stringToLong(String pNumber) {

- Sourav July 12, 2012long result = 0;

String list = "0123456789";

for (int i = 0; i <= pNumber.length()-1; i++) {

char c = (char) pNumber.charAt(i);

int lastValue = list.indexOf(c);

result= result*10 + lastValue;

}

return result;

}