字符串分割

来源:互联网 发布:淘宝网服装女装 编辑:程序博客网 时间:2024/05/16 22:43

题目

连续输入字符串(输出次数为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
原创粉丝点击