fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string trimLeft(string str) {
  5. if (!str.size()) return "";
  6. int i = 0;
  7. while (str[i] == ' ' || str[i] == '0') {
  8. i++;
  9. }
  10.  
  11. return str.substr(i);
  12. }
  13.  
  14.  
  15. bool checkIsValidString(string str) {
  16. for (int i = 0; i < str.size(); i++) {
  17. if (!isdigit(str[i])) {
  18. return false;
  19. }
  20. }
  21.  
  22. return true;
  23. }
  24.  
  25. int myAtoi (string str) {
  26. if (!str.size()) return 0;
  27.  
  28. bool hasNegativeSymbol = str[0] == '-';
  29. bool hasPositiveSymbol = str[0] == '+';
  30. string s = (hasNegativeSymbol || hasPositiveSymbol) ? str.substr(1) : str;
  31.  
  32. s = trimLeft(s);
  33.  
  34. bool isValidInput = checkIsValidString(s);
  35. if (!isValidInput) return 0;
  36.  
  37. int size = s.size(), result = 0, currentPower = 0;
  38.  
  39. for (int i = size - 1; i >= 0; i--) {
  40. int currentNumber = s[i] - '0';
  41. result = result + (currentNumber * pow(10, currentPower++));
  42. }
  43.  
  44. return hasNegativeSymbol ? -result : result;
  45. }
  46.  
  47. int main() {
  48. string s;
  49.  
  50. while (cin >> s) {
  51. cout << myAtoi(s) << endl;
  52. }
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0.01s 5284KB
stdin
45434
    -0042
000042400000
hello how are you 434343
0490-34-0
stdout
45434
-42
42400000
0
0
0
0
434343
0