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#endifchar* 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] = '';    return szT;}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: