如何在不区分大小写的情况下从文本文件中找到单词... [英] How Can I Find A Word From Text File Without Case-Sensitivity ...

查看:159
本文介绍了如何在不区分大小写的情况下从文本文件中找到单词...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不区分大小写的情况下从文本文件中找到单词...例如,我必须在文本文件中找到单词" 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....

推荐答案

以下函数比较两个字符串,不区分大小写:
The following function compares two strings without any case sensitivity:
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.


要查找不区分大小写的单词,请同时更改从文件和word中读取的内容所有的帽子.然后使用strcmp.
To find a word without case sensitivity, change both what you read from the file and word to all caps. Then check the equality with strcmp.


这篇关于如何在不区分大小写的情况下从文本文件中找到单词...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆