//Maxwell Brewer CS1A Chapter 10, p. 592, #18
//
/*******************************************************************************
* PHONEBOOK SEARCH PROGRAM
* _____________________________________________________________________________
* This program allows the user to search for a name in a predefined phonebook.
* The user provides an input string, and the program displays all entries
* from the phonebook that contain the input as a substring (case-sensitive).
*
* INPUT:
* - A name or partial name entered by the user (max 20 characters).
*
* OUTPUT:
* - A list of phonebook entries matching the input string.
*******************************************************************************/
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
// Function prototype
void displayMatching(const string phonebook[], const char* searchTerm);
// Predefined phonebook data
const string phonebook[11] = {
"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"
};
int main() {
// Input buffer for the search term
char searchTerm[21]; // 20 characters max + null terminator
// Prompt the user for input
cout << "Enter a name or part of a name to search in the phonebook (max 20 characters): ";
cin.getline(searchTerm, 21); // Read input up to 20 characters
cout << "\nMatching entries:\n";
cout << "------------------\n";
// Call the function to display matching entries
displayMatching(phonebook, searchTerm);
return 0; // Indicate successful program termination
}
void displayMatching(const string phonebook[], const char* searchTerm) {
bool matchFound = false;
// Iterate through all entries in the phonebook
for (int i = 0; i < 11; i++) {
// Use strstr() to find if the input string is a substring of the current entry
if (strstr(phonebook[i].c_str(), searchTerm)) {
cout << phonebook[i] << endl; // Display the matching entry
matchFound = true;
}
}
// If no match is found, inform the user
if (!matchFound) {
cout << "No matching entries found.\n";
}
}
Ly9NYXh3ZWxsIEJyZXdlciAgICAgICAgICAgICAgICAgQ1MxQSAgICAgICAgICAgICAgICAgICAgQ2hhcHRlciAxMCwgcC4gNTkyLCAjMTgKLy8KLyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKICogUEhPTkVCT09LIFNFQVJDSCBQUk9HUkFNCiAqIF9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCiAqIFRoaXMgcHJvZ3JhbSBhbGxvd3MgdGhlIHVzZXIgdG8gc2VhcmNoIGZvciBhIG5hbWUgaW4gYSBwcmVkZWZpbmVkIHBob25lYm9vay4KICogVGhlIHVzZXIgcHJvdmlkZXMgYW4gaW5wdXQgc3RyaW5nLCBhbmQgdGhlIHByb2dyYW0gZGlzcGxheXMgYWxsIGVudHJpZXMKICogZnJvbSB0aGUgcGhvbmVib29rIHRoYXQgY29udGFpbiB0aGUgaW5wdXQgYXMgYSBzdWJzdHJpbmcgKGNhc2Utc2Vuc2l0aXZlKS4KICogCiAqIElOUFVUOgogKiAgICAtIEEgbmFtZSBvciBwYXJ0aWFsIG5hbWUgZW50ZXJlZCBieSB0aGUgdXNlciAobWF4IDIwIGNoYXJhY3RlcnMpLgogKiAKICogT1VUUFVUOgogKiAgICAtIEEgbGlzdCBvZiBwaG9uZWJvb2sgZW50cmllcyBtYXRjaGluZyB0aGUgaW5wdXQgc3RyaW5nLgogKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKi8KCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxzdHJpbmc+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy8gRnVuY3Rpb24gcHJvdG90eXBlCnZvaWQgZGlzcGxheU1hdGNoaW5nKGNvbnN0IHN0cmluZyBwaG9uZWJvb2tbXSwgY29uc3QgY2hhciogc2VhcmNoVGVybSk7CgovLyBQcmVkZWZpbmVkIHBob25lYm9vayBkYXRhCmNvbnN0IHN0cmluZyBwaG9uZWJvb2tbMTFdID0gewogICAgIkFsZWphb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