Advertisement
wagner-cipriano

Using stringstream to separate words in C++

Nov 19th, 2024
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | Software | 0 0
  1. #include <iostream>
  2. // #include <regex>
  3. #include <string>
  4. #include <sstream>
  5.  
  6. int main()
  7. {
  8.     std::string line = "Joao amava Teresa que amava Raimundo que amava Maria que amava Joaquim que amava Lili que nao amava ninguem. Joao foi pra os Estados Unidos, Teresa para o convento, Raimundo morreu de desastre, Maria ficou para tia, Joaquim suicidou-se e Lili casou com JPinto Fernandes que nao tinha entrado na historia.";
  9.     std::stringstream words(line);
  10.     std::string word, wordSeparated;
  11.     while (getline(words, word, ' ')) {
  12.         std::stringstream wordsSeparated(word);
  13.         while (getline(wordsSeparated, wordSeparated, '-')) {
  14.             std::cout << wordSeparated << std::endl;
  15.         }
  16.     }
  17. }
  18.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement