// // example of recursion: // function to perform search-and-replace on list of strings, // plus main program illustrating its use. // // input: none. // output: result of calling function on a predefined input. // #include #include #include #include "seq.h" // Note the "", not <>, causes this // directory to be searched as well as the // standard places implied by <>. using namespace std; // Given a list of strings, return a (new) list with one string // substituted for the other string. // Post: returns a list of strings that consists of SL with // all occurrences of oldString replaced by newString. Seq subst(const string & oldString, const string & newString, const Seq & SL) { if (SL.empty()) return SL; else { if (SL.hd() == oldString) return Seq(newString, subst(oldString, newString, SL.tl())); else return Seq(SL.hd(), subst(oldString, newString, SL.tl())); } } // Print contents of a list of strings. // Post: all elements of SL printed to standard output, one per line. void print(const Seq & SL) { if (!SL.empty()) { cout << SL.hd() << endl; print(SL.tl()); } } // Test program. int main(void) { Seq testString = Seq("ab", Seq("cd", Seq("ab", Seq("ef", Seq())))); print(subst("ab", "AB", testString)); return EXIT_SUCCESS; }