码迷,mamicode.com
首页 > 其他好文 > 详细

将字符串逆序

时间:2017-12-02 11:22:37      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:ret   for   argv   etl   pause   def   har   lin   字符串逆序   

将字符串逆序

 

#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] = { \0 };
    cin.getline(a, MAX_STR_LEN, \n);
    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;
}

 

将字符串逆序

标签:ret   for   argv   etl   pause   def   har   lin   字符串逆序   

原文地址:http://www.cnblogs.com/lsgxeva/p/7945712.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!