C++ - To UpperCase?

Discussion in 'C++' started by GRoll21, Oct 18, 2005.

  1. GRoll21

    GRoll21 Guest

    I know there is a function but I cannot seem to find it. There should
    be a way to uppercase a char right?

    Here is what I got.

    cout << "Enter title of a book for look up: ";
    cin >> firstString;

    if (strcmp(toupper(firstString), toupper(bookTitle[index])) == 0)

    then i check the book they enter to a book in my bookTitle array. How
    can I make it so it puts both firstString and bookTitle to uppercase?
    Any help would be great! Thanks!
     
    GRoll21, Oct 18, 2005
    #1
    1. Advertising

  2. GRoll21 wrote:
    > I know there is a function but I cannot seem to find it. There should
    > be a way to uppercase a char right?


    'toupper' should do it. But you don't need to uppercase a char, do you?
    You need to uppercase a whole array of them, probably...

    > Here is what I got.
    >
    > cout << "Enter title of a book for look up: ";
    > cin >> firstString;
    >
    > if (strcmp(toupper(firstString), toupper(bookTitle[index])) == 0)


    What's a 'firstString'?

    > then i check the book they enter to a book in my bookTitle array. How
    > can I make it so it puts both firstString and bookTitle to uppercase?


    So, it's an array, eh? Then you need to iterate through the array and
    apply 'toupper' to every element. It's that simple.

    V
     
    Victor Bazarov, Oct 19, 2005
    #2
    1. Advertising

  3. GRoll21

    GRoll21 Guest

    firstString is the book the user inputs.

    cout << "Enter title of a book for look up: ";
    cin >> firstString;
     
    GRoll21, Oct 19, 2005
    #3
  4. GRoll21

    Guest

    GRoll21 wrote:
    > I know there is a function but I cannot seem to find it. There should
    > be a way to uppercase a char right?
    >
    > Here is what I got.
    >
    > cout << "Enter title of a book for look up: ";
    > cin >> firstString;
    >
    > if (strcmp(toupper(firstString), toupper(bookTitle[index])) == 0)
    >
    > then i check the book they enter to a book in my bookTitle array. How
    > can I make it so it puts both firstString and bookTitle to uppercase?
    > Any help would be great! Thanks!


    If you want to make the string all uppercase then just go through the
    string and convert all the characters:

    const char* strtoupper(string str)
    {
    for (int i=0;i<str.size();i++)
    str = toupper(str);
    return str.c_str();
    }
     
    , Oct 19, 2005
    #4
  5. GRoll21

    Jay Nabonne Guest

    On Tue, 18 Oct 2005 15:48:13 -0700, GRoll21 wrote:

    > I know there is a function but I cannot seem to find it. There should
    > be a way to uppercase a char right?
    >
    > Here is what I got.
    >
    > cout << "Enter title of a book for look up: ";
    > cin >> firstString;
    >
    > if (strcmp(toupper(firstString), toupper(bookTitle[index])) == 0)
    >
    > then i check the book they enter to a book in my bookTitle array. How
    > can I make it so it puts both firstString and bookTitle to uppercase?


    If you just want to compare, you can use the case-insensitive version of
    strcmp. Now is it strcmpi or stricmp? (I'm not sure. :)

    - Jay
     
    Jay Nabonne, Oct 19, 2005
    #5
  6. GRoll21

    jalkadir Guest

    I wrote a set o f C++ classes that handle different types of string
    manipulations one of string manipulations that is most needed by
    programmers is the ability to convert the string to upper case:

    const std::string& jme::strtools::toUpper( const std::string& s ) {
    tmp = s;
    tmp = this->trimIt( tmp );
    for ( std::string::iterator i = tmp.begin(); i != tmp.end(); ++i )
    {
    *i = std::toupper( *i );
    }
    return tmp;
    }
    we are developing this library to be released under the GNU linces. So,
    if you are interested you can via AIM=jalqadir
    for a free copy of this library.

    Have fun!
     
    jalkadir, Oct 19, 2005
    #6
  7. GRoll21

    Guest

    jalkadir wrote:
    > const std::string& jme::strtools::toUpper( const std::string& s ) {


    If you are working on a temporary copy anyway, why not simply declare
    it like this:

    const std::string& jme::strtools::toUpper( std::string s )

    Saves a few lines of code and does the same...

    > tmp = s;


    Where is tmp declared?

    > tmp = this->trimIt( tmp );


    This function is called "toUpper()", why does it trim the string as
    well?

    > for ( std::string::iterator i = tmp.begin(); i != tmp.end(); ++i )
    > {
    > *i = std::toupper( *i );
    > }
    > return tmp;
    > }


    How about:

    std::transform( tmp.begin(), tmp.end(), tmp.begin(),
    (int(*)(int))std::toupper );

    > we are developing this library to be released under the GNU linces. So,
    > if you are interested you can via AIM=jalqadir
    > for a free copy of this library.


    Why not check it into sf.net or similar?
    I'd like to see this library and monitor it's progress - maybe even
    contribute. It would be easier that way.

    Cheers,
    Andre
     
    , Oct 19, 2005
    #7
  8. GRoll21

    Jack Klein Guest

    On Tue, 18 Oct 2005 23:57:54 GMT, Jay Nabonne
    <> wrote in comp.lang.c++:

    > On Tue, 18 Oct 2005 15:48:13 -0700, GRoll21 wrote:
    >
    > > I know there is a function but I cannot seem to find it. There should
    > > be a way to uppercase a char right?
    > >
    > > Here is what I got.
    > >
    > > cout << "Enter title of a book for look up: ";
    > > cin >> firstString;
    > >
    > > if (strcmp(toupper(firstString), toupper(bookTitle[index])) == 0)
    > >
    > > then i check the book they enter to a book in my bookTitle array. How
    > > can I make it so it puts both firstString and bookTitle to uppercase?

    >
    > If you just want to compare, you can use the case-insensitive version of
    > strcmp. Now is it strcmpi or stricmp? (I'm not sure. :)
    >
    > - Jay


    There is no case-insensitive version of strcmp() in the standard C or
    standard C++ library. What you are not sure of is the name of some
    particular non-standard function on some specific implementation, or
    perhaps the different names of several non-standard functions on
    different implementations.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Oct 19, 2005
    #8
  9. GRoll21 wrote:
    > firstString is the book the user inputs.
    >
    > cout << "Enter title of a book for look up: ";
    > cin >> firstString;
    >


    That is obvious, what Victor meant (I assume) is have you declared
    firstString like this

    std::string firstString;

    or something like this

    char firstString[99];

    These details matter.

    But in either case the answer is similar. You have a function toupper
    which converts a single character to uppercase, you have a string or a
    char array which is basically a whole bunch of characters, so you have
    to write a loop which uses toupper on each character in your string or
    char array.

    This is called programming, there always comes a point where the
    standard functions run out and you have to write your own. You've just
    reached it.

    john
     
    John Harrison, Oct 19, 2005
    #9
  10. GRoll21

    Neil Cerutti Guest

    On 2005-10-19, jalkadir <> wrote:
    > I wrote a set o f C++ classes that handle different types of
    > string manipulations one of string manipulations that is most
    > needed by programmers is the ability to convert the string to
    > upper case:
    >
    > const std::string& jme::strtools::toUpper( const std::string& s ) {
    > tmp = s;
    > tmp = this->trimIt( tmp );
    > for ( std::string::iterator i = tmp.begin(); i != tmp.end(); ++i )
    > {
    > *i = std::toupper( *i );
    > }
    > return tmp;
    > }


    You should not return a reference or pointer to automatic storage
    class variables. tmp is destroyed when the function returns,
    leaving the caller with a useless reference to a nonexistent
    string.

    --
    Neil Cerutti
     
    Neil Cerutti, Oct 19, 2005
    #10
  11. GRoll21

    Shezan Baig Guest

    Neil Cerutti wrote:
    > > const std::string& jme::strtools::toUpper( const std::string& s ) {
    > > tmp = s;
    > > tmp = this->trimIt( tmp );
    > > for ( std::string::iterator i = tmp.begin(); i != tmp.end(); ++i )
    > > {
    > > *i = std::toupper( *i );
    > > }
    > > return tmp;
    > > }

    >
    > You should not return a reference or pointer to automatic storage
    > class variables. tmp is destroyed when the function returns,
    > leaving the caller with a useless reference to a nonexistent
    > string.



    I suspect 'tmp' is a member of the 'strtools' class (or maybe a global
    variable). In any case, since it is not declared in function scope, it
    will *not* be destroyed when the function returns, so the reference is
    still valid.

    As to why we need this 'tmp' global variable is a good question. It
    definitely kills any possibillity of doing multi-threaded programming.
    I suspect the original author mistakenly thought it might be
    significantly more efficient to use an existing variable.

    -shez-
     
    Shezan Baig, Oct 19, 2005
    #11
  12. GRoll21

    Neil Cerutti Guest

    On 2005-10-19, Shezan Baig <> wrote:
    >
    > Neil Cerutti wrote:
    >> > const std::string& jme::strtools::toUpper( const std::string& s ) {
    >> > tmp = s;
    >> > tmp = this->trimIt( tmp );
    >> > for ( std::string::iterator i = tmp.begin(); i != tmp.end(); ++i )
    >> > {
    >> > *i = std::toupper( *i );
    >> > }
    >> > return tmp;
    >> > }

    >>
    >> You should not return a reference or pointer to automatic storage
    >> class variables. tmp is destroyed when the function returns,
    >> leaving the caller with a useless reference to a nonexistent
    >> string.

    >
    >
    > I suspect 'tmp' is a member of the 'strtools' class (or maybe a
    > global variable). In any case, since it is not declared in
    > function scope, it will *not* be destroyed when the function
    > returns, so the reference is still valid.


    Ah. Whoops I didn't notice it went undeclared. Thanks for the
    correction.

    --
    Neil Cerutti
     
    Neil Cerutti, Oct 19, 2005
    #12
  13. GRoll21 wrote:

    > I know there is a function but I cannot seem to find it. There should
    > be a way to uppercase a char right?
    >
    > then i check the book they enter to a book in my bookTitle array. How
    > can I make it so it puts both firstString and bookTitle to uppercase?
    > Any help would be great! Thanks!


    There is the CRT function/macro toupper from <cctype>, which converts a
    single character to upper case, using the C locale.
    There is the ctype facet of the C++ locale from <locale> which has a member
    toupper, which does the same, but using the C++ locale.
    There is a global toupper function in <locale>, which is essentially a
    shortcut to the ctype facet.

    To work on entire strings at the same time, I recommend the Boost String
    Algorithm library, available in Boost 1.32.0 and later.
    http://www.boost.org/doc/html/string_algo/usage.html#id1290831

    --
    Sebastian Redl
     
    Sebastian Redl, Oct 19, 2005
    #13
  14. GRoll21

    Jay Nabonne Guest

    On Tue, 18 Oct 2005 21:57:02 -0500, Jack Klein wrote:

    >
    > There is no case-insensitive version of strcmp() in the standard C or
    > standard C++ library. What you are not sure of is the name of some
    > particular non-standard function on some specific implementation, or
    > perhaps the different names of several non-standard functions on
    > different implementations.


    Well, that explains it, then. :)
     
    Jay Nabonne, Oct 19, 2005
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ruby
    Replies:
    1
    Views:
    2,555
    Ray Dixon [MVP]
    Jul 31, 2003
  2. Guest

    uppercase

    Guest, Feb 20, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    500
    Nathan Sokalski
    Feb 20, 2006
  3. Sudeep Batra - Bond 007

    Converting to uppercase using Perl5Substitution

    Sudeep Batra - Bond 007, Apr 7, 2004, in forum: Java
    Replies:
    1
    Views:
    389
    Andrew Thompson
    Apr 7, 2004
  4. Sudeep Batra - Bond 007

    Converting string to uppercase using Perl5Substitution

    Sudeep Batra - Bond 007, Apr 7, 2004, in forum: Java
    Replies:
    2
    Views:
    1,666
    Andrew Thompson
    Apr 8, 2004
  5. natG
    Replies:
    4
    Views:
    9,676
Loading...

Share This Page