【華爲機試在線訓練】字符串分隔

題目

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

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

輸出描述:
輸出到長度爲8的新字符串數組

輸入

abc
123456789

輸出

abc00000
12345678
90000000

思路

使用substr函數

C++代碼

#include<iostream>
#include<string>
using namespace std;

void fun(string str) 
{
	while (str.size() > 8) 
    {
		cout << str.substr(0, 8) << endl;
		str = str.substr(8);
	}
	if (str.size() > 0) 
    {
		cout << str.append(8 - str.size(), '0') << endl;
	}
}
int main() 
{
	string str[2];
	cin >> str[0];
	cin >> str[1];
	fun(str[0]);
	fun(str[1]);

	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章