This is an extra credit assignment worth a small amount of extra points (25). You should do this assignment if you finished the other work quickly. If you do not have time to do this work, don't worry because there will be many future opportunities to pick up a small amount of extra points if needed. Last day to submit this to Dora is Tuesday, Sep 17. No late work accepted.
"On my honor, as a University of Colorado at Boulder student, I have neither given nor received unauthorized assistance on this work."
Even one violation of this Honor Pledge will result in a grade of F for the entire course and possible additional sanctions from the campus Honors Code Committee.
Extra Credit Program 3:
The purpose of this assignment is to make sure that you know how to write a program that contains functions that meet specific requirements. WARNING: Your work will receive no points if you solve the problem without implementing the two required C++ functions listed below (two versions of a C++ search function, one with a return value of bool and one with a return value of int). Moreover, your functions must work correctly for any valid arguments (not just for the particular numbers that your main program uses).
For the assignment, write a program that a person can use to search through a name to see whether the letters of some other word appear in order (but not necessarily consecutively) within the name. The program should ask for the name (with no spaces), then ask for the word, and then print a message indicating whether the letters of the word appear in order within the name. For example:
Please type a name with no spaces: monicalewinsky Please type a word: mole Answer: Yes, the word mole appears in order within monicalewinskeyThe reason for the Yes answer is because the name monicalewinskey contains at some point: an m, and then later there is an o, and then later there is an l, and later there is an e. If the name does not contain the letters of the word in order, then a negative message is printed. For example:
Please type a name with no spaces: michaelmain Please type a word: chile Answer: No, the word chile does not appear in order within michaelmainAt the end of each computation, the program should ask the user whether he or she wants to continue (doing another computation).
Your program must include functions with these exact prototypes. Notice that both functions are called search. That is permitted so long as the functions have different parameter lists.
int search(string name, char c, int start_position); // This search function starts at the given start_postion of the // name and searches for the first occurrence of the character c. // If it finds the character c, then the return value is the // position of that c within the name. If it does not find the // character c, then the return value is -1. // Remember that the position of the first string character is // always zero (not 1), so for example: // search("michaelmain", 'm', 0) returns 0 // search("michaelmain", 'm', 4) returns 7 // search("michaelmain", 'm', 10) returns -1 bool search(string name, string word); // This search function checks to see whether the letters of the word // appear in order (but not necessarily consecutively) in the name. // If so, then the function returns true; otherwise the function // return false. // Two special cases that you want to be sure to test: // (1) If the word is the empty string (with no characters), then // the answer is always true. (2) If the name is empty but the word // is not empty, then the answer is always false. Here are examples: // search("monicalewinsky", "mole") returns true // search("michaelmain", "chile") returns false // search("michaelmain", "") returns true // search("", "chile") returns false // search("", "") returns true
#include <string>-- notice that this is just
cout << "Please type a name without spaces: "; cin >> name;
s[i]. And to get the length of a string s, use the notation:
s.length( ). Remember that the first character of a string is at position zero, so that the valid indexes of the string run from
Lis the string's length.