【code】字符串分割

c语言

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LEN 100
void func(char* input) {
	int len = strlen(input);
	int start = 0;
	int end = 7;
	while (end <= len) {
		for (; start <= end; start++) {
			putchar(input[start]);
		}
		putchar('\n');
		start = start + 1;
		end = end + 8;
	}
	int result = len - start + 1;
	if (result > 0) {
		start = start - 1;
		for (int i = 1; i <= 8; i++) {
			if (i <= result) {
				putchar(input[start]);
				start++;
			}
			else {
				putchar('0');
			}
		}
		putchar('\n');
	}
}
int main() {
	char input1[MAX_LEN] = {'0'};
	char input2[MAX_LEN] = {'0'};
	while (scanf_s("%s%s", input1, 100, input2, 100) != EOF) {
		func(input1);
		func(input2);
	}
	return 0;
}

C++

#include<iostream>
#include<vector>
using namespace std;
#define LENTH 8
int main() {
	string input1, input2;
	while (cin >> input1) {
		cin >> input2;
		vector<string> str;
		str.push_back(input1);
		str.push_back(input2);
		for (int i = 0; i < 2; i++) {
			string temp = str[i];
			int result = temp.size() % LENTH;
			int row = temp.size() / LENTH;
			for (int j = 0; j < LENTH - result; j++) {
				temp += "0";
			}
			if (result > 0) {
				row++;
			}
			for (int j = 0; j < row; j++) {
				cout << temp.substr(j * LENTH, LENTH) << endl;
			}
		}
	}
	return 0;
}

发布了46 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zzy296753977/article/details/102528096