#include #include #include "bsearch.h" char *names[] = { "George M. Blum", "Ruth B. Boland", "Bill C. Johnson", "David P. Moses", "Debra S. Rice", "John A. Smith", "Paul S. Wang"}; const len = sizeof(names)/sizeof(char*); char *getkey(char *s, unsigned int i) { for ( int j = 1; j < i; j++) // skipping fields while ( *s++ != ' ' ); // delimiter is single space return(s); } int cmp_lastname(char *ent, char *key) { char *k = getkey(ent, 3); return(strcmp(k, key)); } main() { int j; j = bsearch(names, "Rice", 0, len-1, cmp_lastname); if (j >= 0 ) cout << names[j] << "\n"; j = bsearch(names, "Doe", 0, len-1, cmp_lastname); if (j < 0 ) cout << "Doe not found\n"; }