问题描述
如何在不区分大小写的情况下从文本文件中找到单词...例如,我必须在文本文件中找到单词" C row". .但是存在" c 行" ...那么我怎么能找到这个词.我不知道用户会输入 CROW , Crow , crow , croW , crOW 等,但我希望该用户输入它,答案将是相同的(应打印文本文件中的相同单词)
例如,我有一个代码可以从行的开头查找任何单词:
How can I find a word from text file without case-sensitivity ...For example I have to find the word "Crow" in text file...but there exists "crow"...Then how can I find that word.???
I don''t know that user will enter CROW , Crow , crow, croW , crOW , etc. but I want that user enter it and answer will be same (Same word from text file should be printed)
For Example I have a code to find any word from the starting of line :
void meaning()
{
ifstream input( "all.txt" );
for(string line; getline( input, line ); )
{
if (line.find("crow") == 0)
{
cout<<line<<endl;
}
}
}
这里的单词"是用户给出的任何单词...请帮助我解决问题....
Here "word" is any word given by user...Please help me to solve problem....
推荐答案
bool CompareWithoutCaseSensitivity(string s1, string s2)
{
bool r = false;
string tmp = "", tmp2 = "";
int i;
for (i = 0; i < s1.length(); i++)
{
tmp = tmp + (char)toupper(s1.at(i));
}
for (i = 0; i < s2.length(); i++)
{
tmp2 = tmp2 + (char)toupper(s2.at(i));
}
return (tmp == tmp2);
}
如果发现任何缺陷,可以改进此代码.我刚刚写了它,但我不能保证这是最好的方法.
You can improve this code if you find any defects. I wrote it just now and I can''t assure you this is the best method.
这篇关于如何在不区分大小写的情况下从文本文件中找到单词...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!