Thread: Searching a file for a String Occurence

  1. #16
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    One thing that I would like to do now is make the search not case sensitive, any ideas out there?

    I think if I convert the camparing to upper or lower case before comparison; that will work, will it not?

    Cheers!

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you convert both to monocase before comparing, that will work.

    There is no standard case-insensitive comparison function, but most OSes have a nonstandard one of their own.

  3. #18
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    You could always implement your own version using toupper();
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #19
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    Quote Originally Posted by CodeMonkey View Post
    You could always implement your own version using toupper();
    That's what I'm working on, problem is: I want a generic function to do so. The first class uses strings the second is a structure of char arrays.

    ideas appreciated.

  5. #20
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Use a template that accepts any object for which operator[] can be called.

    *then termination might be tricky*
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #21
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    Quote Originally Posted by CodeMonkey View Post
    Use a template that accepts any object for which operator[] can be called.

    *then termination might be tricky*
    those are my thoughts as well, once I have something that quasi works, i'll post it to help get it refined. I usually go about accomplishing tasks the hard way

    Thanks!

  7. #22
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Come to think of it, I suggest you just overload the function. You could bank on std::string::operator[](this->size()) to be 0, as the standard demands for c_str() -- but the former is not always true. The obvious solution is NocaseComp(str1, str2, num), but that seems inelegent, especially if only two data types are seriously under consideration... and for such a small function.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #23
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Actually, is this ugly and slow?
    Code:
    template<class string_type>
    bool strcompi(string_type & a, string_type & b)
    {
        std::string one(a);
        std::string two(b);
        std::for_each(one.begin(), one.end(), std::toupper);
        std::for_each(two.begin(), two.end(), std::toupper);
        return one == two;
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CodeMonkey
    Actually, is this ugly and slow?
    The use of std::toupper (as opposed to the global toupper) is problematic in this case, though the exact reason eludes me at the moment. (Type mismatch? I know this has been discussed before.) Josuttis suggests just defining a function that compares characters without regard for case, and then use say, equal(), e.g.,
    Code:
    bool noCaseEqualChar(char lhs, char rhs)
    {
        return std::toupper(lhs) == std::toupper(rhs);
    }
    
    bool noCaseEqual(const std::string& lhs, const std::string& rhs)
    {
        return lhs.size() == rhs.size()
            && std::equal(lhs.begin(), lhs.end(), rhs.begin(), noCaseEqualChar);
    }
    We could use function object template classes instead and thus not have to resort to the different function names.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    It was actually much easier than that:
    Code:
    strupr((char *)studentName.c_str());   // This is the string member of the class
    strupr((char *)obj.studentName);       // This is the char[] member of the struct
    Then compare them, but since I don't want to change the original data I am going to copy them into temp variables, put them in uppercase, then compare them.

    Is there any problem with doing it this way?

    Thanks everyone for you help!

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Phyxashun
    Is there any problem with doing it this way?
    Yes. The c_str() member function returns a const char* for a good reason: you are not allowed to modify the string via the pointer returned. Blatantly ignoring the const qualifier by means of a cast is just sticking your fingers in your ears to try and block out the fire alarm in the hope that the fire will not actually burn you.

    Besides that, a potential problem is that strupr() is non-standard.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #27
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    I'm not worried about strupr not being standard.

    but is this correct?
    Code:
    char *pStudent = new char[sizeof(studentName)+1];
    strcpy(pStudent, studentName.c_str());
    
    char *pOStudent = new char[sizeof(obj.studentName)];
    strcpy(pOStudent, obj.studentName);
    
    strupr(pStudent);
    strupr(pOStudent);
    ...
    ...
    ...
    delete [] pStudent;
    delete [] pOStudent;
    it works, and doesn't modify the original.

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Phyxashun
    but is this correct?
    Almost. If the second new[] throws std::bad_alloc then you would get a memory leak since you would not be able to use delete[] on the pointer returned by the first new[].

    Why are you so resistant to using std::string and my suggestion?

    EDIT:
    Wait a minute. studentName is a std::string, so that use of sizeof is wrong. You would use studentName.size() (or studentName.length()) instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #29
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    And why doesn't this logic hold?

    Code:
    if(pStudent == pOStudent)
    ...
    There is no reason why haven't tried std::string yet. From what I can tell that will be the route I take. Right now I am just fiddling around with the function, to see what I can come up with.

    Thanks!

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Phyxashun
    And why doesn't this logic hold?
    As in it does not actually do a case insensitive comparison? That would be because pStudent and pOStudent are pointers, so you are merely comparing addresses. You would need to use strcmp() if you really want to use C-style strings.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM