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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


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
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date