Advertisement
chevengur

Вводный курс: основы C++ | Урок 3: Возврат значения и ошибки

Aug 30th, 2023 (edited)
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. vector<string> SplitIntoWords(string text) {
  8.     vector<string> words;
  9.     string word;
  10.     for (char c : text) {
  11.         if (c == ' ') {
  12.             if (!word.empty()) {
  13.                 words.push_back(word);
  14.                 word.clear();
  15.             }
  16.         }
  17.         else {
  18.             word += c;
  19.         }
  20.     }
  21.     if (!word.empty()) {
  22.         words.push_back(word);
  23.     }
  24.     return words;
  25. }
  26.  
  27. int main() {
  28.     string query;
  29.     getline(cin, query);
  30.     auto words = SplitIntoWords(query);
  31.     for (string word : words) {
  32.         cout << '[' << word << ']' << endl;
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement