// FILE: DoReplac.cpp // REPLACES ALL OCCURRENCES OF A TARGET CHARACTER IN A STRING // WITH A NEW CHARACTER. USES "apstring" void do_replace (apstring& test_string, // INOUT: string in which replacement occurs bool& success) // OUT: indicates success or failure // Pre: test_string is assigned a value. // Post: target characters om test_string are changed to a new // character and success is true if the target is found. // Otherwise, test_string is unchanged and success if false { // Local data char target_char; // input - character to be replaced char new_char; // input - new character to replace with int pos_target; // location of target if found // Enter character data needed. cout << "Enter character to be replaced: "; cin >> target_char; cout << "Enter replacement character: "; cin >> new_char; // Locate target string and perform replacement if possible pos_target = test_string.find (target_char); if (pos_target != -1) { success = true; // target_char found do { test_string[pos_target] = new_char; pos_target = test_string.find (target_char); } while (pos_target != -1); // continue to look } else success = false; } // end do_replace