Searching String Problem

jjnkln
Last edited on
First of all you must to include header <string> instead of <cstring>.

The search can be done for example the following way

1
2
3
4
5
string *p = array;

while ( p != array + LINES && str != p->substr( 0, str.size() ) ) ++p;

if ( p != array + LINES ) cout << "The target record is " << *p << endl;


Last edited on
ajipvkaj eu fzzzt
Last edited on
@Duoas

Uh, that search won't work for OP, and it is confusingly written


What confusing did you find and why you decide the search won't work?
Vlad thank you your code worked perfectly. Yet, how do i make it accept a typo so like Vlak would still display the contents of Vlad?
Last edited on
You were shown a code with a typo. Moreover it is incorrect code because it does not search strings that start with the target string. For example let assume that you entered string "Lo". There is no such a record in the array that starts with "Lo". However the proposed code points out the first record.

As for the typo I mentioned then you are using variable n that was not initialized

if (array[n].find( str ) != string::npos)
Last edited on
The problem is that it is difficult to distinguish whether a typo was done or it is a valid string.

For example let assume that "Joa" was entered If you have records "Joe..." and "Joan..." how to understand what record the user want to find?
Last edited on
There is an idea to use a "distance" between strings and find the record with the least distance.
Last edited on
do you know how to do that?? i have no idea how to write this for a typo
As I said you can search a record that is the nearest to the target record. However in this case the result can contain several records.
Topic archived. No new replies allowed.