-2

I am working a program that searches for a string (in this case a name) in a file. I wanted the program not to be case sensitive but strcmp is. I was thinking to convert bot the file and user input to lowercase. But that would be inefficient. Any other suggestions to overcome this?
This is a fraction of code to just get an idea of the program

    cout << "\n Enter the Guests name: ";
    cin.getline(look_4_person, 256);  //name that is being looked up
    cout << "\n Searching... \n";
    while(!name_file.eof())
    {
        ++place;
        name_file.getline(person,255);
        if(strcmpi (person,look_4_person)==0)
        {
        found=place;   
        }
    }
    cout << "\n" << look_4_person << " is number " << found << 
            " on the list \n";
gsamaras
  • 71,951
  • 46
  • 188
  • 305
O2Addict
  • 147
  • 11

2 Answers2

2

was thinking to convert bot the file and user input to lowercase. But that would be inefficient. Any other suggestions to overcome this?

To re-think this.

This is a typical way of dealing with case sensitiveness. I mean converting both strings (file name and user input) to lowercase.

This takes O(n), where n = max(filename.size, userInput.size).

As for performance, the filename and the user input are usually tiny data, thus I am pretty sure that converting them to lower case, will be definitely not be the bottleneck of your algorithm.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
0
while(!name_file.eof()){
        ++place;
        name_file.getline(person,256);
        for(i=0; i<200; i++)
      {
        person[i] = tolower(person[i]);  //changes to lower case to compare
      }
        if(strcmp (person,look_4_person)==0){   //compares 
        found=place;                            

        }
O2Addict
  • 147
  • 11