Huawei Written Questions: String Segmentation

 

Title description

• To input character strings continuously, please split each character string by the length of 8 and output it to a new character string array
.

Enter description:

Enter character strings consecutively (enter twice, each character string length is less than 100)

Output description:

Output to a new string array of length 8

Example 1

Input

abc
123456789

Output

abc00000
12345678
90000000
#include <iostream>
#include <string>

using namespace std;

void func1(string s) {
    int len = s.length();
    cout << s;
    if (len < 8) {
        for (int i = 0; i < 8 - len; ++i) {
            cout << '0';
        }
    }
    cout << endl;
}

void func2(string s) {
    int len = s.length();
    int index = len % 8;

    for (int i = 0; i < len - index; ++i) {
        cout << s[i];
        if ((i + 1) % 8 == 0) cout << endl;
    }
    if(index != 0)
        func1(s.substr(len - index, len - 1));
}

int main() {
    string s;
    while (cin >> s) {
        if (s.size() <= 8) func1(s);
        else func2(s);
    }
    return 0;
}

 

Published 34 original articles · Like 10 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/weixin_41111088/article/details/104771205