刷题牛客——华为

https://www.nowcoder.com/ta/huawei

HJ4 字符串分隔

描述
•连续输入字符串,请按长度为8拆分每个输入字符串并进行输出;
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
(注:本题有多组输入)
输入描述:
连续输入字符串(输入多次,每个字符串长度小于等于100)

输出描述:
依次输出所有分割后的长度为8的新字符串


解答1:

#include
using namespace std;

int main() {
    string str;
    while(cin >> str) {
        while(str.size() > 8) {
            cout << str.substr(0, 8) << endl;
            str = str.substr(8);
        }
        str.resize(8, '0');
        cout << str << endl;
    }
    return 0;
}

这里主要用到两个函数

substr()

你可能感兴趣的:(刷题,华为,c++,算法)