华为OJ——字符串分隔

来源:互联网 发布:免费手机屏幕录制软件 编辑:程序博客网 时间:2024/05/18 03:58

字符串分隔

题目描述

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

输入描述:

连续输入字符串(输入2,每个字符串长度小于100)

输出描述:

输出到长度为8的新字符串数组

输入例子:

abc

123456789

输出例子:

abc00000

12345678

90000000

解答代码:

#include<iostream>#include<cstring>using namespace std;int main(){    char s1[120];    char s2[120];    int i,length;    int count=0;    cin.getline(s1,120);    cin.getline(s2,120);    //处理第一个字符串    length=strlen(s1);    if(length!=0)    {        int n=0;        int flag=0;        if(length%8!=0)        {            n=(8-length%8);            flag=1;        }        for(i=0; i<length; i++)        {            count++;            cout<<s1[i];            if(count==8)            {                count=0;                cout<<endl;            }        }        if(flag)        {            for(i=0; i<n; i++)                cout<<'0';            cout<<endl;        }    }    //处理第二个字符串    count=0;    length=strlen(s2);    if(length!=0)    {        int n=0;        int flag=0;        if(length%8!=0)        {            n=(8-length%8);            flag=1;        }        for(i=0; i<length; i++)        {            count++;            cout<<s2[i];            if(count==8)            {                count=0;                cout<<endl;            }        }        if(flag)        {            for(i=0; i<n; i++)                cout<<'0';            cout<<endl;        }    }    return 0;}

输入的两个字符串都是一样的处理过程,个人认为这种题目意义不大。

0 0