From 81a20a3e93a6c14c0077bf34940cea8e60a2ab04 Mon Sep 17 00:00:00 2001 From: Tanya Chauhan Date: Fri, 14 Aug 2020 00:58:11 +0530 Subject: [PATCH] Added the algorithm for atoi() --- atoi.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 atoi.cpp diff --git a/atoi.cpp b/atoi.cpp new file mode 100644 index 0000000..b88c6c4 --- /dev/null +++ b/atoi.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; +int myAtoi(string str) { + int n=str.size(); + int i=0; + while(i='0' && str[i]<='9')){ + return 0; + } + long long ans=0; + for(i;i='0' && str[i]<='9'){ + if(sign==1){ + if(INT_MAX/10'7'){ + return INT_MAX; + } + } + if(sign==-1){ + if(INT_MAX/10'8'){ + return INT_MIN; + } + } + ans*=10; + ans+=str[i]-'0'; + } + else{ + break; + } + } + return sign*ans; + } \ No newline at end of file