一个老问题,颠倒字符串,想看看专家推荐的做法

一个老问题,颠倒字符串,想看看专家推荐的做法
谢谢

4个回答

http://codepad.org/j7GOwAc5

Output:

!dlrow olleh

调试通过

基本都是这样
char *revstr(char *str, size_t len)
{

char    *start = str;
char    *end = str + len - 1;
char    ch;

if (str != NULL)
{
    while (start < end)
    {
        ch = *start;
        *start++ = *end;
        *end-- = ch;
    }
}
return str;

}

 #include <iostream>

using namespace std;

void myreverse(char * des, char * src)
{
    int n = strlen(src);
    *(des + n) = '\0';
    for (int i = 0; i < n; i++)
    {
        *(des + i) = *(src + n - i - 1);
    }
}

int main()
{
    char * source = new char[100];
    char * result = new char[100];
    cin >> source;
    myreverse(result, source);
    cout << result;
}

std::reverse().

立即提问