-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmyString.cpp
More file actions
34 lines (31 loc) · 914 Bytes
/
myString.cpp
File metadata and controls
34 lines (31 loc) · 914 Bytes
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
/****************************************************************************
FileName [ myString.cpp ]
PackageName [ util ]
Synopsis [ Customized string processing functions ]
Author [ Chung-Yang (Ric) Huang ]
Copyright [ Copyleft(c) 2007-present LaDs(III), GIEE, NTU, Taiwan ]
****************************************************************************/
#include <string>
#include <ctype.h>
using namespace std;
// Convert string "str" to integer "num". Return false if str does not appear
// to be a number
bool
myStr2Int(const string& str, int& num)
{
num = 0;
size_t i = 0;
int sign = 1;
if (str[0] == '-') { sign = -1; i = 1; }
bool valid = false;
for (; i < str.size(); ++i) {
if (isdigit(str[i])) {
num *= 10;
num += int(str[i] - '0');
valid = true;
}
else return false;
}
num *= sign;
return valid;
}