File tree 1 file changed +7
-1
lines changed
1 file changed +7
-1
lines changed Original file line number Diff line number Diff line change @@ -5,12 +5,18 @@ using namespace std;
5
5
6
6
// https://stackoverflow.com/questions/4654636/how-to-determine-if-a-string-is-a-number-with-c
7
7
8
+ // support positive and negative integer
8
9
bool is_number (const std::string& s){
9
- return !s.empty () && s.find_first_not_of (" 0123456789" ) == std::string::npos;
10
+ if (s.empty ()) return false ;
11
+ if (s.size () > 1 && s[0 ] == ' -' && s.find_first_not_of (" 0123456789" , 1 ) == std::string::npos) return true ;
12
+ return s.find_first_not_of (" 0123456789" ) == std::string::npos;
10
13
};
11
14
12
15
int main () {
13
16
cout << is_number (" 123" ) << endl; // 1
17
+ cout << is_number (" -123" ) << endl; // 1
18
+ cout << is_number (" -123abc" ) << endl; // 0
19
+ cout << is_number (" -" ) << endl; // 0
14
20
cout << is_number (" abc" ) << endl; // 0
15
21
cout << is_number (" 123abc" ) << endl; // 0
16
22
return 0 ;
You can’t perform that action at this time.
0 commit comments