1

Can someone give me an example of how to partial name search or make the search not case sensitive. I typed these functions to search by last name but I was wondering how to partial name search/non-case sensitive. Thank you

  int Search()
    {
        for(int i = 0 ; i < Size ; i++)
            if (Target == List[i].LastName)
                return i;
        return -1;
    }
    void LookUp_Student()
    {
        string Target;
        int Index;
        cout << "\nEnter a name to search for (Last Name): ";
        getline(cin,Target);
        Index = Search(List,Size,Target);
        if (Index == -1)
            cout << Target <<" is not on the list.\n" ;
        else
            Print_Search(List[Index]);
    }
user3078999
  • 11
  • 1
  • 6
  • You may find this question and answer helpful http://stackoverflow.com/questions/313970/stl-string-to-lower-case at least for the case insensitive aspect – mathematician1975 Dec 08 '13 at 09:29
  • Use toupper (or tolower) to make it case insensitive. Then use find to search the strings for substrings. – graham.reeds Dec 08 '13 at 09:30

1 Answers1

0

You can use a custom function to do a string case insensitivity comparison :

#include <algorithm>
//...
bool compare_str(const string& lhs, const string& rhs)
{
   return lhs.size() == rhs.size() &&
          std::equal(lhs.begin(), lhs.end(), rhs.begin(),
                // Lambda function 
                [](const char& x, const char& y) {
                // Use tolower or toupper
                  return toupper(x) == toupper(y); 
                   }
                );
}

And then use it like:

if (compare_str(Target,List[i].LastName) )
{
// a match
}

Ref : std::equal

P0W
  • 46,614
  • 9
  • 72
  • 119