#include <iostream> #include <cstring> #include<string> using namespace std; boo
ID: 3539323 • Letter: #
Question
#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;
for (int i=0; i<11; i++)
{
strPtr = strstr(people[i].c_str(), input.substr(1).c_str());
if (strPtr != NULL )
{
cout<<" "<<people[i]<<endl;
found = true;
is_found[i] = true;
}
}
}
else
{
for (int i=0; i<11; i++)
{
bool match = true;
string str = people[i];
int value = str.find(input[0]);
for(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<<" "<<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<< " People and Phone numbers"<<endl;
cout<< "Enter name or phone number: ";
cin >> lookUp;
cout<<"result: "<<endl;
bool found = false;
bool output = false;
for(int i=0; i<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 was found"<<endl;
return 0;
}
I need this to expand the search pattern with the potential one character of letter or digit typos. Only one character maybe a typo, in any position Try these test patterns should get the following results: 0-9 is 0x30-0x39
a-z is 0x41-0x5A
A-Z is 0x61-0x7A (or lower case it)
No other characters in the search pattern may be changed.
Explanation / Answer
#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;
for (int i=0; i<11; i++)
{
strPtr = strstr(people[i].c_str(), input.substr(1).c_str());
if (strPtr != NULL )
{
cout<<" "<<people[i]<<endl;
found = true;
is_found[i] = true;
}
}
}
else
{
for (int i=0; i<11; i++)
{
bool match = true;
string str = people[i];
int value = str.find(input[0]);
for(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<<" "<<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<< " People and Phone numbers"<<endl;
cout<< "Enter name or phone number: ";
cin >> lookUp;
cout<<"result: "<<endl;
bool found = false;
bool output = false;
for(int i=0; i<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 was found"<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.