c++ - Typo searching for at least 1 character -


the original prompt was: write program keeps track of speakers bureau. program should use structure store following data speaker: name telephone number speaking topic fee required

the program should use array of @ least 10 structures. should let user enter data array, change contents of element, , display data stored in array. program should have menu-driven user interface. input validation: when data new speaker entered, sure user enters data fields. no negative amounts should entered speaker s fee.

the added prompt was: need expand search pattern potential 1 character of letter or digit typos. 1 character maybe typo, in position try these test patterns should following results:

0-9 0x30-0x39 a-z 0x41-0x5a a-z 0x61-0x7a (or lower case it) 

and can't added prompt work current program.

no other characters in search pattern may changed.

#include <iostream> #include <cstring> #include<string>  using namespace std; bool print_one_typo(string input, string people[11], bool is_found[11]) {     bool found = false;     if (input[0] == '?')     {         char *strptr = null;         (int = 0; < 11; i++)         {             strptr = strstr(people[i].c_str(), input.substr(1).c_str());             if (strptr != null)             {                 cout << "\t" << people[i] << endl;                 found = true;                 is_found[i] = true;             }         }     }     else     {         (int = 0; < 11; i++)         {             bool match = true;             string str = people[i];             int value = str.find(input[0]);             (int k = 0; k < input.length(); k++)             {                 if (input[k] != '?' && input[k] != str[value++])                 {                     match = false;                     break;                 }             }             if (match && !is_found[i])             {                 found = true;                 cout << "\t" << people[i] << endl;             }         }     }     return found; }  int main() {     string people[11] = { "becky warren, 555-1223",         "joe looney, 555-0097",         "geri palmer, 555-8787",         "lynn presnell, 555-1225",         "holly gaddis, 555-8878",         "sam wiggins, 555-0998",         "bob kain, 555-8712",         "tim haynes, 555-7676",         "warren gaddis, 555-9037",         "jean james, 555-9223",         "ron palmer, 555-7227" };      bool is_found[11] = { false };     string lookup;     int i;     cout << "\t people , phone numbers" << endl;     cout << "enter name or phone number: ";     cin >> lookup;     cout << "result: " << endl;     bool found = false;     bool output = false;     (int = 0; < lookup.length(); i++)     {         string local = lookup;         found = print_one_typo(local.replace(i, 1, 1, '?'), people, is_found);         if (found) output = true;     }     if (!output)         cout << "no matching product found" << endl;      return 0; } 

i think code overthinks problem. also, didn't specify if "typo" means "wrong character", or fuller range includes dropped characters or inserted characters.

if looking matches 0 or 1 incorrect characters, otherwise same length, think code should do:

bool print_one_typo(string input, string people[11], bool is_found[11]) {     (int = 0; < 11; i++)     {         if ( is_found[i] )              // skip if had been found?             continue;          if ( input.length() != people[i].length() )  // same length?             continue;                                // no:  skip it.          int    typos = 0;         size_t len   = input.length();          (size_t j = 0; j != len && typos < 2; j++)              if ( input[j] != people[i][j] )                  typos++;          if (typos < 2)    // fewer 2 typos:  have winner!  return it.         {             is_found[i] = true;             return true;         }      }       return false; }  

this code skips strings differ in length, or you're filtering out via is_found[] array. not sure why you're doing that, preserved bit of original code.

if finds 2 strings same length, compares them character character, counting typos. if sees 2 or more typos, skips next one. otherwise, takes first string that's same length fewer 2 typos.


Comments