c++ - Returning null char array with rewriting Java's String.toUpperCase() -
this question has answer here:
- convert string in c++ upper case 24 answers
i java programmer , have deep love of syntax, regarding string object. c++, have tried recreate touppercase() method java has. problem returns string object has empty/null char array.
string string::touppercase() { char *a = new char[this->length + 1]; memset(a, 0, this->capacity + 1); memcpy(a, this->characters, this->length); (int = 0; < strlen(this->characters); i++) { toupper(a[i]); } return *new string(a); }
you have few memory problems attempt, logical one. need return copy of string characters being upper case is:
std::string str = "my original string"; std::string mycopy(str); std::locale loc; std::transform(mycopy.begin(), mycopy.end(), mycopy.begin(), [&](char c) { return std::toupper(c, loc); });
Comments
Post a Comment