Search code examples
c++stringalgorithmperformancestrcmp

Making strcmp() case insensitive without using strcmpi() (C++)


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";

Solution

  • 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;                            
    
            }