String case reverse function in C++ This reverses the case of a character in a string (a string being an array of characters). How does it look? Anything I can improve on? Let's get critical here.

#include &lt;iostream&gt;
#include &lt;string&gt;
//
// return a new string
//
std::string camel_case_rev(const std::string &amp;word)
{
        std::string ret = "";

        for(unsigned i = 0; i &lt; word.size(); i++) {
                if(word[i] &lt;= 'Z' &amp;&amp; word[i] &gt;= 'A') {
                        ret += tolower(word[i]);
                }
                else if(word[i] &lt;= 'z' &amp;&amp; word[i] &gt;= 'a') {
                        ret += toupper(word[i]);
                }
                else {
                        ret += word[i];
                }
        }
        return ret;
}


int main(int argc, char **argv)
{
        if(argc &lt; 2) {
                std::cerr &lt;&lt; "USAGE: " &lt;&lt; argv[0] &lt;&lt; " " &lt;&lt; "&lt;string&gt;\n";
                return 1;
        }

        std::string a = camel_case_rev(argv[1]);

        std::cout &lt;&lt; a &lt;&lt; "\n";

        return 0;
}
This seems like a perfect opportunity to leverage the standard library. I would make a function to toggle a single character and then use std::transform on top of that. Not only is it more consise, it will be more performant (you should use reserve to preallocate the resulting string -- otherwise the string may allocate over and over again).

#include &lt;cctype&gt;
#include &lt;string&gt;
#include &lt;algorithm&gt;

int toggle_case(int chr) {
    return std::islower(chr) ? std::toupper(chr) : std::tolower(chr);
}

std::string toggle_case(std::string str) {
    std::transform(std::begin(str), std::end(str), std::begin(str), toggle_case);
    return str;
}


Note that standard islower and isupper were used instead of comparison against character literals. Not only is it more compact and clearer, C++ does not guarantee that 'A', 'B', ..., 'Z' are contiguous.

Also, since you're (presumably) including the C++ version of ctype.h, cctype, you should be using std::toupper and std::tolower. C++ versions of C headers are allowed to place symbols in the global namespace, but they're only required to place them in std. There's no existing compiler I know of that doesn't also place them in the global namespace, but it can never hurt to strive for portabiltiy, especially when it's easy.

return std::islower(chr) ? std::toupper(chr) : std::tolower(chr);


This might look a little weird, but std::tolower and std::toupper are guaranteed to have no effect if no opposite-cased character exists for the input. In other words, std::tolower(c) is guaranteed to have no effect unless std::toupper(c) is true. To me, this makes sense and this short-cutted logic seems fairly natural. I could see this being rather weird to read though, so it might be better as a more explicit conditional chain.

I should also mention that the code makes an implicit copy of the string via the parameter. If you wanted to make the copy more explicit, you could, but that would actually be potentially hampering performance since it would disable movement into the parameter. In short, with the implicit copy, there's a potential that a copy will not actually happen. With an explicit copy, a copy will always happen.