-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2feb_atoi.cpp
51 lines (43 loc) · 871 Bytes
/
2feb_atoi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//{ Driver Code Starts
//Initial template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution{
public:
/*You are required to complete this method */
int atoi(string s) {
int index = 0;
int ans = 0;
bool neg = 0;
if(s[index] == '-'){
neg = 1;
++index;
}
for(; index < s.size(); index++){
if(isdigit(s[index])){
ans *= 10;
ans += (s[index] - '0');
}
else{
return -1;
}
}
return ans * (neg ? -1 : 1);
}
};
//{ Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
Solution ob;
cout<<ob.atoi(s)<<endl;
}
}
// } Driver Code Ends