不使用 strcmpi() (C++) 使 strcmp() 不区分大小写
Making strcmp() case insensitive without using strcmpi() (C++)
我正在开发一个在文件中搜索字符串(在本例中为名称)的程序。我希望程序不区分大小写,但 strcmp 是。我正在考虑将 bot 文件和用户输入转换为小写。但那将是低效的。还有其他解决这个问题的建议吗?
这是一小部分代码,只是为了了解程序
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";
was thinking to convert bot the file and user input to lowercase. But that would be inefficient. Any other suggestions to overcome this?
重新思考一下。
这是处理区分大小写的典型方法。我的意思是将两个字符串(文件名和用户输入)都转换为小写。
这需要 O(n)
,其中 n = max(filename.size, userInput.size)
。
至于性能,文件名和用户输入通常是很小的数据,因此我很确定将它们转换为小写肯定会而不是 算法的瓶颈。
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;
}
我正在开发一个在文件中搜索字符串(在本例中为名称)的程序。我希望程序不区分大小写,但 strcmp 是。我正在考虑将 bot 文件和用户输入转换为小写。但那将是低效的。还有其他解决这个问题的建议吗?
这是一小部分代码,只是为了了解程序
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";
was thinking to convert bot the file and user input to lowercase. But that would be inefficient. Any other suggestions to overcome this?
重新思考一下。
这是处理区分大小写的典型方法。我的意思是将两个字符串(文件名和用户输入)都转换为小写。
这需要 O(n)
,其中 n = max(filename.size, userInput.size)
。
至于性能,文件名和用户输入通常是很小的数据,因此我很确定将它们转换为小写肯定会而不是 算法的瓶颈。
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;
}