Results are out! Find what you need...fast. Get quick advice or join the chat
Hey! Sign in to get help with your study questionsNew here? Join for free to post

C++ function

Announcements Posted on
Has your uni kept its promises? Vote now to have your say (you could even win an Apple Watch). 29-05-2015
  1. 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.
  2. 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 );
  3. 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?
  4. Offline

    ReputationRep:
    Yes, s.size() would work fine also.
  5. 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

Taxing sugar

Should sweet foods subsidise healthy ones?

Article updates
x

Think you’ll be in clearing or adjustment?

Hear direct from unis that want to talk to you

Get email alerts for university course places that match your subjects and grades. Just let us know what you’re studying.

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