fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <stdexcept>
  4. using namespace std;
  5.  
  6. int intFromString(const string &s) {
  7. stringstream stream(s);
  8. int result;
  9. if (stream >> result)
  10. return result;
  11. throw std::invalid_argument(string("Can't convert string: \"") + s + "\" to int");
  12. }
  13.  
  14. int reversedInt(const string &s) {
  15. return intFromString(string(s.rbegin(), s.rend()));
  16. }
  17.  
  18. int main() {
  19. string s;
  20.  
  21. while(cin >> s) {
  22. try {
  23. cout << "Normal=" << intFromString(s) << " Reversed=" << reversedInt(s) << endl;
  24. } catch (const invalid_argument &e) {
  25. cout << e.what() << endl;
  26. }
  27. }
  28. return 0;
  29. }
Success #stdin #stdout 0s 3236KB
stdin
001234
432200
234234dr234234
vsd23423
2342fs
stdout
Normal=1234   Reversed=432100
Normal=432200   Reversed=2234
Normal=234234   Reversed=432432
Can't convert string: "vsd23423" to int
Can't convert string: "sf2432" to int