https://rentry.org/PPP2_p71

// Code derived from Stroustrup's PPP2 book
// § 3.5.1 An example: detect repeated words
// -beginning on p 71

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::string;

int main()
{
  cout << "Please enter words (use ctrl+d to end input)\n";
  string previous = " ";  // previous word; initialized to “not a word”
  string current;         // current word

  while (cin >> current) {    // read a stream of words
    if (previous == current)  // check if the word is the same as last
      cout << "repeated word: " << current << '\n';

    previous = current;
  }
}

build & run:

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

PrevUpNext

Edit
Pub: 18 Jan 2023 05:59 UTC
Edit: 29 Apr 2023 06:19 UTC
Views: 648