Hey there Sign in to join this conversationNew here? Join for free

C++ function

Announcements Posted on
Study Help needs new mods! 14-04-2014
We're up for a Webby! Vote TSR to help us win. 10-04-2014
    • Thread Starter
    • 0 followers
    Offline

    ReputationRep:
    I need a function that takes a reference to a string object and that converts the contents of the string to uppercase.

    Code:
    string function(string s) {
    	for (int i = 0; i < strlen(s); i++)
    		s[i]=toupper(s[i]);
    	return s;
    }
    When I compile, I get the error: Cannot convert 'string' to 'const char *'. Please help.
    • 0 followers
    Offline

    ReputationRep:
    Hello Thomas.

    strlen takes a C-style string as an argument, not a std::string, therefore you must use s.c_str() as the argument when calling strlen.

    Here is the function prototype
    Code:
    size_t strlen ( const char * str );
    • Thread Starter
    • 0 followers
    Offline

    ReputationRep:
    (Original post by Jiggle)
    Hello Thomas.

    strlen takes a C-style string as an argument, not a std::string, therefore you must use s.c_str() as the argument when calling strlen.

    Here is the function prototype
    Code:
    size_t strlen ( const char * str );
    I can use s.size() either, right?
    • 0 followers
    Offline

    ReputationRep:
    Yes, s.size() would work fine also.
    • 0 followers
    Offline

    ReputationRep:
    Code:
    string::iterator it;
    for ( it=str.begin() ; it < str.end(); it++ )
         *it = toupper(*it);
    

Reply

Submit reply

Register

Thanks for posting! You just need to create an account in order to submit the post
  1. this can't be left blank
    that username has been taken, please choose another Forgotten your password?

    this is what you'll be called on TSR

  2. this can't be left blank
    this email is already registered. Forgotten your password?

    never shared and never spammed

  3. this can't be left blank

    6 characters or longer with both numbers and letters is safer

  4. this can't be left empty
    your full birthday is required
  1. By completing the slider below you agree to The Student Room's terms & conditions and site rules

  2. Slide the button to the right to create your account

    Slide to join now Processing…

    You don't slide that way? No problem.

Updated: May 11, 2012
Article updates
Reputation gems:
You get these gems as you gain rep from other members for making good contributions and giving helpful advice.