home   articles   tags   browse code   

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;
}
 

Tags: c++
 
boli on Aug 17th, 2009 2:43 am said:
why not take str by reference ?
 



 

 



Related Articles
 


home  |  privacy policy  |  terms of use  |  contact  


©2010, Zedwood Digital