C++ strtoupper function


Continuing on with our efforts to replicate the php string manipulation with C++ and the STL, I have implemented strtoupper() and strtolower();
#include <iostream>
#include <string>
 
using namespace std;
 
string strtolower(string str);
string strtoupper(string str);
 
int main(int argc, char *argv[])
{
    cout << strtoupper("loWer") << endl;//LOWER
    cout << strtolower("loWer") << endl;//lower
    cout << strtoupper("UPPER") << endl;//UPPER
    cout << strtolower("UPPER") << endl;//upper
    cout << strtoupper("`az{") << endl; //`AZ{
    cout << strtolower("@AZ[") << endl; //@az[
}
 
string strtoupper(string str)
{
    int leng=str.length();
    for(int i=0; i<leng; i++)
        if (97<=str[i]&&str[i]<=122)//a-z
            str[i]-=32;
    return str;
}
string strtolower(string str)
{
    int leng=str.length();
    for(int i=0; i<leng; i++)
        if (65<=str[i]&&str[i]<=90)//A-Z
            str[i]+=32;
    return str;
}

code snippets are licensed under Creative Commons CC-By-SA 3.0 (unless otherwise specified)

boli on 2009-08-17 08:43:14
why not take str by reference ?