#1
  1. No Profile Picture
    Senior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Posts
    42
    Rep Power
    0

    C++ / Case insensitivity


    there's no method in <string> to convert text to upper/lower case but i need to read some input from a file and search for a word in this text. Cases can be mixed - is there an easy way to do this or do i have to write a struct with new traits?

    thanks

    Comments on this post

    • Gran Roguismo agrees
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248

    Re: C++ / Case insensitivity


    Originally posted by lux
    there's no method in <string> to convert text to upper/lower case
    Use the transform function to convert a string to lowercase or uppercase:

    Code:
    #include <string>
    #include <algorithm>
    #include <cctype>
    
    ....
    
    transform(sString.begin(), sString.end(),  // Start, end to transform on
                     sString.begin(), // Destination start
                     tolower // Operation to perform on string
    );
  4. #3
  5. No Profile Picture
    Senior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Posts
    42
    Rep Power
    0
    thankyou :)
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248
    Edited the code above so that you can figure out what the parameters mean.
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248
    Just noticed you want to do a case-insensitive find. For that, you might as well have code like this:

    Code:
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <cctype>
    using namespace std;
    
    bool nocase_compare (char c1, char c2)
    {
        return toupper(c1) == toupper(c2);
    }
    
    
    ....
    string::iterator pos;
    pos = search (s1.begin(),s1.end(),   // haystack
                           s2.begin(),s2.end(),   // needle
                            nocase_compare);    // comparison criterion
    if (pos == s1.end()) {
       // Not found
    } else {
       // String is found
    }

IMN logo majestic logo threadwatch logo seochat tools logo