-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathstringToInt.cpp
More file actions
55 lines (44 loc) · 1.27 KB
/
stringToInt.cpp
File metadata and controls
55 lines (44 loc) · 1.27 KB
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
52
53
54
55
#include <string>
#include <iostream>
#include <climits>
int stringToInteger(const std::string& str)
{
int result = 0;
bool isNegative = false;
int i = 0;
//check str is negative or not
if(str[0] == '-')
{
isNegative = true;
i++;
}
for(; i<str.size(); ++i)
{
if(std::isdigit(str[i]))
{
// to check the overflow is exist or not
// if INT_MAX > (result * 10 + (str[i] - '0')) then there is a overflow
// if(result * 10 + (str[i] - '0') > INT_MAX) // but this will not work
// because result * 10 + (str[i] - '0') if result is cause overflow then there
// will be undefined behaviour so it is good idea to compare it with result
if(result > (INT_MAX - (str[i] - '0')) / 10)
{
std::cout<<"overflow detected"<<std::endl;
return isNegative ? INT_MIN : INT_MAX;
}
result = result * 10 + (str[i] - '0');
}
else
return 0;
}
if(isNegative)
result = -result;
return result;
}
int main()
{
std::cout<<stringToInteger("123")<<std::endl;
std::cout<<stringToInteger("99999999999999999999999999");
std::endl(std::cout);
return 0;
}