Advertisement
Spocoman

08. Invalid Input

Oct 29th, 2023 (edited)
855
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     string str, word = "", digit = "";
  9.     getline(cin, str);
  10.     str += ' ';
  11.     int sumNumbers = 0;
  12.     vector<string> words;
  13.  
  14.     for (int i = 0; i < str.length(); i++) {
  15.         if (isdigit(str[i]) || str[i] == '-' && isdigit(str[i + 1])) {
  16.             digit += str[i];
  17.         }
  18.         else if (str[i] != ' ') {
  19.             word += str[i];
  20.         }
  21.         else {
  22.             if (digit != "") {
  23.                 sumNumbers += stoi(digit);
  24.                 digit = "";
  25.             }
  26.             else {
  27.                 words.push_back(word);
  28.                 word = "";
  29.             }
  30.         }
  31.     }
  32.  
  33.     cout << sumNumbers << endl;
  34.  
  35.     for (auto& w : words) {
  36.         cout << w << ' ';
  37.     }
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement