字符串分割

题目

连续输入字符串(输出次数为N,字符串长度小于100),请按长度为8拆分每个字符串后输出到新的字符串数组,长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。

输入

首先输入数字n,表示要输入多少个字符串。连续输入字符串(输出次数为N,字符串长度小于100)。

输出

按长度为8拆分每个字符串后输出到新的字符串数组,长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。

样例输入

2 abc 123456789

样例输出

abc00000
12345678
90000000

思路

正常输出字符串,添加换行(也可以保存下来)

代码

#include <iostream>
#include <string>
using namespace std;

void splitAndFill(string str,char ch='0')
{
    int counter=0;
    //每隔八位换行
    for(string::iterator iter=str.begin(); iter!=str.end(); ++iter)
    {
        if(counter%8!=7||iter==str.begin())
        {
            cout<<*iter;
            counter++;
        }
        else
        {
            cout<<*iter;
            cout<<endl;
            counter=0;
        }
    }
    //如果有空余则补齐8位
    if(counter!=0)
    {
        for(int i=counter; i<8; ++i)
        {
            cout<<ch;
        }
        cout<<endl;
    }
}

int main()
{
    string str;
    int times=0;
    cin>>times;
    while(times!=0)
    {
        cin>>str;
        splitAndFill(str,'0');
        times--;
    }
    return 0;
}

描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值