Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Intermediate
Nov 22, 2005

Porting Code from Windows to Linux

This tip provides a substitute for the __strrev function, because it's not available under Linux. This is useful if you are porting existing code from Windows to Linux.

Suppose you want to use __strrev functionality with ordinary char* C functions and not STL strings? This tip provides two separate implementations: a standard implementation, and one for use with STL. For both, the input and the output is still char*.


//strrev the standard way
// the following directives to make the code portable 
// between windows and Linux.

#if !defined(__linux__)
#define __strrev strrev
#endif

char* strrev(char* szT)
{
    if ( !szT )                 // handle null passed strings.
        return "";
    int i = strlen(szT);
    int t = !(i%2)? 1 : 0;      // check the length of the string .
    for(int j = i-1 , k = 0 ; j > (i/2 -t) ; j-- )
    {
        char ch  = szT[j];
        szT[j]   = szT[k];
        szT[k++] = ch;
    }
    return szT;
}

// strrev STL way .
char* StrRev(char* szT)
{
    string s(szT);
    reverse(s.begin(), s.end());
    strncpy(szT, s.c_str(), s.size());
    szT[s.size()+1] = '\0';
    return szT;
}
Elsayed Elghadban
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap