Impliment Atoi

PHOTO EMBED

Fri Sep 30 2022 03:32:08 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

class Solution
{
  public:
    /*You are required to complete this method */
    int atoi(string A)
    {
        //Your code here
    int res=0;
    int cnt=0;
    for(int i=0; i<A.size();i++)
      {
        int c=(A[i]-'0');
        if(c==-3)
        {
            cnt++;
            
        }
        else if(c>=0 && c<=9)
        {
            res=res*10+(A[i]-'0');
        }
        else return -1;
      }
      if(cnt>1)
            {
            return -1;
            }
      if(cnt==1)
            {
            cout<<"-";
            }
    return res;
    }
    
    
};
content_copyCOPY

https://practice.geeksforgeeks.org/problems/implement-atoi/1