Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //{ 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 str) {
- //Your code here
- int ans=0;
- bool f=true;
- bool isneg=false;
- if(str[0]=='-')
- {
- isneg=true;
- }
- int k=0;
- for(char c:str)
- {
- if(c=='-' && k==0)
- {
- }
- else if(c>='0' && c<='9')
- {
- ans=ans*10+(c-'0');
- }
- else
- {
- f=false;
- break;
- }
- k++;
- }
- if(f)
- {
- if(isneg)
- {
- ans=-1*ans;
- }
- return ans;
- }
- else
- {
- return -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
Advertisement
Add Comment
Please, Sign In to add comment