Department of Computer Science | Institute of Theoretical Computer Science | CADMO

Theory of Combinatorial Algorithms

Prof. Emo Welzl and Prof. Bernd Gärtner

// Program: string_matching2.cpp // find the first occurrence of an input pattern within the // input text, and output the text read so far #include #include // strings are sequences of characters // with array functionality #include // for std::noskipws // POST: returns window, after removing the first character and // appending next as a new last character std::string advance (std::string window, const char next) { int last_index = window.length()-1; for (int i=0; i\n"; return 1; } // search pattern const std::string pattern = argv[1]; // "empty" text window of pattern length std::string window (pattern.length(), '\0'); // read characterwise from standard input until // search pattern was found std::cin >> std::noskipws; // don't skip whitespaces! char c; // next character to be read while (std::cin >> c) { std::cout << c; window = advance (window, c); if (window == pattern) break; } std::cout << "\n"; return 0; }