I've been searching for some more information on this topic, and can't seem to find the answer I'm looking for, so I hope you can help!
Part of an assignment I'm working on is to write a program that searches an array of strings (address book), and returns matches if a full or partial match is found. I'm able to do it easily using an array of C-Strings, with the strstr() function running through a for loop and setting the pointer to the result of running the user input keyword into the array (see below).
My question is, how would I be able to do this, if at all, utilizing String objects? I also need to take into consideration there being more than one possible match. Is this the most efficient way of working this program out as well? I've already submitted my working version, I'm just curious as to some other methods to accomplish the same task!
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
bool isFound = false; // Flag to indicate whether contact is found
const int SIZE = 11; // Size of contacts array
const int MAX = 50; // Maximum characters per row
char contacts[SIZE][MAX] = {
"Jig Sawyer, 555-1223",
"Michael Meyers, 555-0097",
"Jason Vorhees, 555-8787",
"Norman Bates, 555-1212",
"Count Dracula, 555-8878",
"Samara Moran, 555-0998",
"Hannibal Lector, 555-8712",
"Freddy Krueger, 555-7676",
"Leather Face, 555-9037",
"George H Bush, 555-4939",
"George W Bush, 555-2783"
};
char *ptr = NULL; // Pointer to search string within contacts
char input[MAX]; // User search input string
// Get the user input
cout << "Please enter a contact to lookup in the address book: ";
cin.getline(input,MAX);
// Lookup contact(s)
for (int i=0; i<SIZE; i++)
{
ptr = strstr(contacts[i], input);
if (ptr != NULL)
{
cout << contacts[i] << endl;
isFound = true;
}
}
// Display error message if no matches found
if (!contactFound)
cout << "No contacts found." << endl;
return 0;
}
As you can tell, I like horror movies :)