Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 :

C++
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....
Posted
Updated 23-May-14 18:47pm
v5

The following function compares two strings without any case sensitivity:
C#
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.
 
Share this answer
 
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.
 
Share this answer
 
Comments
Crack-Mind-Coder 24-May-14 0:36am    
please give example...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900