Software & Finance





Visual C++ - String Reverse





The source code for reversing a string is given on this page.

 


Source Code


 

bool mystrreverse(char *buf, int len)

{

    int l = (len/2);

    if((len % 2) == 1)

        l++ ;

    for(int i = 0; i < l; i++)

    {

        // swap first and last character

        char ch = buf[i];

        buf[i] = buf[len - 1 - i];

        buf[len - 1 - i] = ch;

    }

    return true;

}

 

   

void main()

{

    char buf[24] = "nmlkjihgfedcba";

    int len = strlen(buf);

    mystrreverse(buf, len);

    std::cout << buf << "\n";

 

}

Output


 

abcdefghijklmn