-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtractNumFromString.cpp
53 lines (42 loc) · 1.41 KB
/
ExtractNumFromString.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
52
53
#include<bits/stdc++.h>
using namespace std;
long long convert(string str) {
long long num = 0;
for(int i = 0; i < str.size(); i++){
if(str[i] == '9') // * If the num consists of a 9 then it cannot be our ans
return -1;
// * Writing : num *= 10 + str[i] WILL NOT WORK!!!!
num = num * 10 + str[i] - '0';
}
// cout << "Num is: " << num << endl;
return num;
}
long long ExtractNumber(string sentence) {
string temp = "";
long long num = -1;
for(int i = 0; i < sentence.size(); i++) {
if(isdigit(sentence[i])){
while(isdigit(sentence[i])) temp += sentence[i++];
}
if(i != 0 && !isdigit(sentence[i]) && isdigit(sentence[i - 1])) {
// * Converted string to integer
long long tempNum = convert(temp);
// * Overwrite the larger one
if(tempNum > num) num = tempNum;
temp.clear();
}
else if(isdigit(sentence[i]) && sentence[i] == '9'){
while(i < sentence.size() && isdigit(sentence[i])) i++;
temp.clear();
}
}
// * Its possible that the last num, at or till last index is the ans
// * Which will not be included because
// * the 2nd IF Condition is written that way
if(temp.size() != 0){
long long tempNum = convert(temp);
// * Overwrite the larger one
if(tempNum > num) num = tempNum;
}
return num;
}