目录
题目描述
•连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
输入描述
连续输入字符串(输入多次,每个字符串长度小于100)
输出描述
输出到长度为8的新字符串数组
示例1
输入
abc
123456789
输出
abc00000
12345678
90000000
做题思路
1. 当所输入的字符串str长度小于8时,补齐缺少的0输出即可
2. 当所输入的字符串str长度大于8时,删除并输出str前8位所构成的子串,直至str的长度小于8,按照1中操作处理
AC代码
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str; //记录每次输入的字串
while( getline(cin, str) ){
while( str.size()>8 ){
cout << str.substr(0, 8) << endl;
str = str.substr(8);
}
cout << str;
for(int i=0; i<8-str.size(); i++){
cout<<0;
}
cout << endl;
}
return 0;
}