0

Is it possible to cast a string to a long without library's in c++, and if it is possible, how would you do it?

C++ no library string to long

11th Nov 2019, 1:43 PM
Kai
Kai - avatar
6 Réponses
+ 2
These are in C, the idea is to read each character in the string, do a bit of checking, and decide whether the string forms a valid numerical expression. https://code.sololearn.com/cD9qu2qTd3RV/?ref=app https://code.sololearn.com/czLw2SdUUhZX/?ref=app
11th Nov 2019, 4:27 PM
Ipang
+ 1
It is possible if we would go the extra mile writing a custom converter function for example.
11th Nov 2019, 2:59 PM
Ipang
+ 1
Thank you, could you post an example? I am pretty new to c++ and didn't found a fitting example
11th Nov 2019, 3:28 PM
Kai
Kai - avatar
+ 1
Thank you, I'll check out later
11th Nov 2019, 4:31 PM
Kai
Kai - avatar
+ 1
Kai What is this `long =0;` in your code? I think you meant `long l = 0;` (forgot the <l>)
16th Nov 2019, 2:15 PM
Ipang
0
Hey, little update, I did it this way: long convertToLong(string x) { long l=0; Int counter=1; for(int i=x.length()-1;i>=0;i--){ l += counter * ((int)x[i] - 48); counter *= 10; } return l; } Thanks for all.
16th Nov 2019, 1:17 PM
Kai
Kai - avatar