here’s my code its giving segmentation fault.
#include
#include
#include
#include
#include
using namespace std;
int stringtoint(string str,int i)
{
if(i==(str.length()-1))
{
int i = str[i]-‘0’;
return i;
}
int sum = pow(10,str.length()-i-1) * (str[i]-‘0’) + stringtoint(str,i+1);
return sum;
}
int main() {
string str;
cin>>str;
int ans = stringtoint(str,0);
cout<<ans;
return 0;
}
and i also want to know why it was giving error when i was using atoi(str[i]) instead of str[i]-‘0’.
thanks