public class Solution {

public int MyAtoi(string str) {

if (string.IsNullOrEmpty(str))

{

return 0;

}

int result = 0;

if (int.TryParse(str, out result))

{

return result;

}

else

{

char[] charArr = str.ToCharArray();

int factor = 1; int i = 0;

```
if (charArr[0] == '+')
{ factor = 1; i = 1; }
else if (charArr[0] == '-')
{ factor = -1; i = 1; }
for (; i < charArr.Length; i++)
{
int temp = charArr[i] - '0';
if (temp >=0 && temp <=9)
{
result = result * 10 + temp;
continue;
}
break;
}
return result * factor;
}
}
```

}