code–>
question–>
https://practice.geeksforgeeks.org/problems/implement-atoi/1
not passing all testcases
Implement atoi implementation
Hey @sheikhhaji18
int atoi(string str)
{
//Your code here
int sum=0;
bool neg=false;
for(int i=0;i<str.length();i++){
if(i==0&&str[i]=='-')neg=true;//added this
else if(str[i]>='0' and str[i]<='9' ){ //added else
// if(str[i]=='-'){never run because outer condn is true
// neg-true;
// }
// else{
sum=sum*10+(str[i]-'0');
// }
}
else{
return -1;
}
}
if(neg)
return -sum;
return sum;
}