Results are out! Find what you need...fast. Get quick advice or join the chat
Hey there Sign in to join this conversationNew here? Join for free

C++ function

Announcements Posted on
Got a question about Student Finance? Ask the experts this week on TSR! 14-09-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?
  2. this can't be left blank
    this email is already registered. Forgotten your password?
  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 joining you agree to our Ts and Cs, privacy policy and site rules

  2. Slide to join now Processing…

Updated: May 11, 2012
New on TSR

Writing your personal statement

Our free PS builder tool makes it easy

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