码迷,mamicode.com
首页 > 编程语言 > 详细

C++刷题——2306: 字符串操作一(串)

时间:2015-05-21 14:16:57      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:c++   iostream   namespace   string   

Description

输入一长度为n的字符串,若其n为偶数,则将字符串从中间反转,若为奇数,则将前后各(n-1)/2个字符反转,中间字符不动。

 

/* All rights reserved.
 * 文件名称:test.cpp
 * 作者:陈丹妮
 * 完成日期:2015年 5 月 21 日
 * 版 本 号:v1.0
 */

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string a;
    while(cin>>a)
    {
        int i,n=0,s;
        for(i=0; a[i]!='\0'; i++)
        {
            n++;
        }
        s=n/2;
        for(i=s-1; i>=0; i--)
        {
            cout<<a[i];
        }
        if((n)%2!=0)
        {
            cout<<a[s];
            for(i=n-1; i>s; i--)
            {
                cout<<a[i];
            }
        }
        else
            for(i=n-1; i>=s; i--)
            {
                cout<<a[i];
            }
        cout<<endl;
    }
    return 0;
}

技术分享

心得体会:仔细看题目,还是希望能够更快更简便的写出程序出来!!继续努力,加油吧

C++刷题——2306: 字符串操作一(串)

标签:c++   iostream   namespace   string   

原文地址:http://blog.csdn.net/nufangdongde/article/details/45890411

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