1. 程式人生 > >華為筆試-字串分割

華為筆試-字串分割

題目描述

連續輸入字串,請按長度為8拆分每個字串後輸出到新的字串陣列;
長度不是8整數倍的字串請在後面補數字0,空字串不處理。

輸入描述:

連續輸入字串(輸入2次,每個字串長度小於100)

輸出描述:

輸出到長度為8的新字串陣列

示例1

輸入

abc

123456789

輸出

abc00000

12345678

90000000

程式碼如下

#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
    string str;
    vector<string> vecstr;
    while(getline(cin,str))
    {
        vecstr.push_back(str);
    }
    for(int i = 0; i < vecstr.size(); i++)
    {
        str = vecstr[i];
        int count = 0;
        for(int j = 0; j < str.size(); j++)
        {
            if(count == 8)
            {
                cout << endl;
                count = 0;
            }
            cout << str[j];
            count++;
        }
        while(count < 8)
        {
            cout <<0;
            count++;
        }
        cout << endl;
    }
    return 0;
}