字符串分隔C++

题目描述

  • 连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
  • 长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
    输入描述:
    连续输入字符串(输入2次,每个字符串长度小于100)

输出描述:

输出到长度为8的新字符串数组

示例1

输入

abc
123456789

输出

abc00000
12345678
90000000


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


int main()
{
	string input;
	while(cin>>input){
		int len = input.length();
		int addNum = 0;
		if(len % 8!=0){
			addNum = 8-len % 8;//addNum表示需要增加的0个数
		}
		for(int i = 0;i<addNum;i++){
			input.append("0");
		}
		for(int j = 0;j<input.length();j++){
			cout << input[j];
			if((j+1)%8 == 0){
				cout << "\n" ;//截取8个数字以后,换行
			}
		}

	}

	return 0;
}

发布了69 篇原创文章 · 获赞 0 · 访问量 1005

猜你喜欢

转载自blog.csdn.net/qq_21209307/article/details/104978360