Question: Laboratory 10: Using C-String and String Classes Write a program that has an array of 11 string objects that hold peoples names and phone numbers.

Laboratory 10: Using C-String and String Classes Write a program that has an array of 11 string objects that hold peoples names and phone numbers. Use the following data: "Alejandra Cruz, 555-1223", "Joe Looney, 555-0097", "Geri Palmer, 555-8787", "Li Chen, 555-1212", "Holly Gaddis, 555-8878", "Sam Wiggins, 555-0998", "Bob Kain, 555-8712", "Tim Haynes, 555-7676", "Warren Gaddis, 555-9037", "Jean James, 555-4939", "Ron Palmer, 555-2783"

Laboratory 10: Using C-String and String Classes The program should ask the user to enter a name or partial name to search for in the array. Any entries in the array that match the string entered should be displayed. For example, if the user enters Palmer the program should display the following names from the list: Geri Palmer, 555-8787 Ron Palmer, 555-2783 This can be done using the C-String Class. Program 10-6, shown in the appendix to this Lab, shows a similar example, indicating how this might be done using the C-String Class. This can also be done using the String Class. For that you will want to make use of the find() method shown on page 589 of Gaddis, and also shown in the appendix to this lab. Tip: If a string Member Function fails to perform the procedure it was asked to do, it returns -1 to the program calling the function.

Laboratory 10: Using C-String and String Classes Using Program 10-6, modify it so that it is able to do a search for the names in our contact list. Note that the Array used here is a two dimensional Array of char The first dimension (row dimension) represents the number of contacts in the Array The second dimension contains the Array of char holding the Contact information for one person Once you have that working, call the instructor so that you may receive credit for having accomplished this. Now, in the remaining time, repeat this search using the String Class instead. You may find that you will be able to execute this program using fewer steps than those used for the C-String class. Note that the Array used here is a one dimensional Array of Strings. Note also that you will be including the string Class instead of the cstring Class. Again, call the instructor when you have this version working OK.

// This program uses the strstr function to search an array. #include #include // For strstr using namespace std; int main() { // Constants for array lengths const int NUM_PRODS = 5; // Number of products const int LENGTH = 27; // String length // Array of products char products[NUM_PRODS][LENGTH] = { "TV327 31 inch Television", "CD257 CD Player", "TA677 Answering Machine", "CS109 Car Stereo", "PC955 Personal Computer" }; char lookUp[LENGTH]; // To hold user's input char *strPtr = NULL; // To point to the found product int index; // Loop counter

// Prompt the usr for a product number. cout << "\tProduct Database "; cout << "Enter a product number to search for: "; cin.getline(lookUp, LENGTH); // Search the array for a matching substring for (index = 0; index < NUM_PRODS; index++) { strPtr = strstr(products[index], lookUp); if (strPtr != NULL) break; } // If a matching substring was found, display the product info. if (strPtr != NULL) cout << products[index] << endl; else cout << "No matching product was found. "; return 0; }

mystring.find(str, x); Returns the first position at or beyond position x where the string str is found in mystring. str may be either a string object or a character array.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!