// Program: string_matching.C
// find the first occurrence of a fixed string within the
// input text, and output the text so far
#include
int main ()
{
// search string
char s[] = "bool";
// determine search string length m
unsigned int m = 0;
for (char* p = s; *p != '\0'; ++p) ++m;
// cyclic text window of size m
char* t = new char[m];
unsigned int w = 0; // number of characters read so far
unsigned int i = 0; // index where t logically starts
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w; // one more character read
j = 0; // restart with first characters
i = (i+1)%m; // of string and window
} else break; // no more characters in the input
else ++j; // match: go to next character
std::cout << "\n";
delete[] t;
return 0;
}