Thread: String Case

  1. #16
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    http://www.gotw.ca/gotw/029.htm

    And also, you could use boost (which may be the better idea anyway versus redefining a string class):

    http://www.boost.org/doc/html/string_algo/quickref.html
    Last edited by Cat; 09-07-2006 at 06:54 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  2. #17
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> But it's just inconvenient to have to create a loop and all that crap!
    Of course, a function would be better if it's being used a lot!

    Code:
    void MyToUpper ( std::string &MyString )
    // or - std::string MyToUpperReturn() if you wanted to return it.
    {
        for ( std::string::size_type i=MyString.begin(); i!=MyString.end(); i++ )
    // or - for ( int i=0; i<(int)MyString.size(); i++ )
        {
            MyString[i] = std::toupper( MyString[i] );
        }
    }
    Last edited by twomers; 09-09-2006 at 06:13 PM.

  3. #18
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can also use std::transform:
    Code:
    #include <string>
    #include <iostream>
    #include <algorithm>
    
    int main()
    {
        std::string s("tHis iS Some SCRewy cRap");
        std::transform(s.begin(), s.end(), s.begin(), toupper);
        std::cout << s << std::endl;
        std::cin.get();
    }

  4. #19
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Quote Originally Posted by anon
    You can also use std::transform:
    Code:
    #include <string>
    #include <iostream>
    #include <algorithm>
    
    int main()
    {
        std::string s("tHis iS Some SCRewy cRap");
        std::transform(s.begin(), s.end(), s.begin(), toupper);
        std::cout << s << std::endl;
        std::cin.get();
    }
    Nice! I like this one the best, very clean! It uses the vector commands, which is cooooool Thanks for this!

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You do realize that transform had already been suggested twice in this thread, right?

  6. #21
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    I didn't see a thing

  7. #22
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Alas, there was no example before!...

  8. #23
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Probably. But had he read about transform when it was suggested without code, it wouldn't have take him 2 days to solve this problem.

    No. Coding is not for the lazy.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #24
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Mkay, maybe because I'm 16, and I have other things to do besides programming? So yes, I am lazy. It's not like I get paid, like you, so please stop caring about other people, and go smoke some weed because you seem tense!

  10. #25
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Thank you son
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #26
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> It's not like I get paid, like you,

    Hmmm, we get paid for posting on the forums!?!? Why hasn't anyone told me that?

  12. #27
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    Quote Originally Posted by Sentral
    Quote Originally Posted by anon
    You can also use std::transform:
    Code:
    #include <string>
    #include <iostream>
    #include <algorithm>
    
    int main()
    {
        std::string s("tHis iS Some SCRewy cRap");
        std::transform(s.begin(), s.end(), s.begin(), toupper);
        std::cout << s << std::endl;
        std::cin.get();
    }
    Nice! I like this one the best, very clean! It uses the vector commands, which is cooooool Thanks for this!
    It might not work. C++ algorithms want a function object or function with C++ linkage. That means that the function needs to support name mangling. If toupper is declared with C linkage then the algorithm might not accept it. You should wrap toupper in a function with C++ linkage or function object.
    Code:
    #include <cctype>
    #include <string>
    #include <iostream>
    #include <algorithm>
    
    // function object
    struct toUpper
    {
        int operator()(int ch)
        {
            return std::toupper(ch);
        }
    };
    
    // function with C++ linkage
    int toLower(int ch)
    {
        return std::tolower(ch);
    }
    
    int main()
    {
        std::string s("tHis iS Some SCRewy cRap");
        std::transform(s.begin(), s.end(), s.begin(), toUpper());
        std::cout << s << std::endl;
        std::transform(s.begin(), s.end(), s.begin(), toLower);
        std::cout << s << std::endl;
        std::cin.get();
    }
    Sumimasen.

  13. #28
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Quote Originally Posted by twomers
    >> It's not like I get paid, like you,

    Hmmm, we get paid for posting on the forums!?!? Why hasn't anyone told me that?
    Yup, I get paid per post!

  14. #29
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You get payed to do nothing and just wait for someone to post some code so that you decide if you like the answer or not?

    Wow!

    *smokes his pipe*
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #30
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Thank you son
    LOL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Replies: 5
    Last Post: 03-05-2009, 11:32 AM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM