C++ - 反转字符串

版权声明:欢迎转载并请注明出处,谢谢~~ https://blog.csdn.net/chimomo/article/details/7713326
/*
 * Created by Chimomo
 */

#include <iostream>

using namespace std;

/**
 * Reverse string.
 * @param str The string to be reversed.
 */
void reverseString(char *str) {
    int n = strlen(str);
    for (int i = 0; i < n / 2; i++) {
        char t = str[i];
        str[i] = str[n - i - 1];
        str[n - i - 1] = t;
    }
}

int main() {
    char str[10] = "123456789";
    reverseString(str);
    cout << str << endl;
    return 0;
}

// Output:
/*
987654321

*/

猜你喜欢

转载自blog.csdn.net/chimomo/article/details/7713326