https://rentry.org/PPP2_p399

// Code derived from Stroustrup's PPP2 book
// § 11.7 Using nonstandard separators
//  -and beginning on p 399

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
  // example input:  As planned, the guests arrived; then,
  cout << "\n>  ";

  string line;
  getline(cin, line);  // read into line

  for (char& ch : line)  // replace each punctuation character by a space
    switch (ch) {
      case ';':
      case '.':
      case ',':
      case '?':
      case '!': ch = ' ';
    }

  stringstream   ss{line};  // make an istream ss reading from line
  vector<string> vs;

  for (string word; ss >> word;)  // read words without punctuation characters
    vs.push_back(word);

  for (auto const& s : vs)
    cout << s << '\n';
}

build & run:

g++ -std=c++20 -O2 -Wall -pedantic ./ch_11/main_p399.cpp && ./a.out

PrevUpNext

Edit
Pub: 09 Apr 2023 06:09 UTC
Edit: 08 May 2023 12:19 UTC
Views: 644