The Student Room Group

C++ function

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

string function(string s) {
for (int i = 0; i < strlen(s); i++)
s=toupper(s);
return s;
}


When I compile, I get the error: Cannot convert 'string' to 'const char *'. Please help.
(edited 11 years ago)
Reply 1
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

size_t strlen ( const char * str );
Reply 2
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

size_t strlen ( const char * str );


I can use s.size() either, right?
Reply 3
Yes, s.size() would work fine also.

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

Quick Reply

Latest

Trending

Trending