将字符串逆序

将字符串逆序

#include <iostream>
#include <string>

#define MAX_STR_LEN 1024

using namespace std;


int main(int argc, char * argv[])
{
#if 0
    // 使用自带的方法进行逆序操作
    string str;
    cin >> str;
    str.assign(str.rbegin(), str.rend());
    reverse(str.begin(), str.end());
    cout << str << endl;
#endif

    // 使用自定义的方法进行逆序操作
    char a[MAX_STR_LEN] = { '' };
    cin.getline(a, MAX_STR_LEN, '
');
    size_t i, j;
    for (i = 0, j = strlen(a) - 1; i < strlen(a) / 2; i++, j--)
    {
        char temp;
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
    cout << a << endl;

    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/lsgxeva/p/7945712.html