C++ 字符串分割

作者 : admin 本文共366个字,预计阅读时间需要1分钟 发布时间: 2024-06-17 共1人阅读

描述

•输入一个字符串,请按长度为8拆分每个输入字符串并进行输出;

•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。

输入描述:

连续输入字符串(每个字符串长度小于等于100)

输出描述:

依次输出所有分割后的长度为8的新字符串

示例1

输入:abc

输出:abc00000

#include 
using namespace std;

int main() {

    std::string str {};
    std::cin >> str;

    while (str.length() % 8) {
        str.push_back('0');
    }

    if (!str.length()) {
        return 0;
    }

    for (int i = 0; i < str.size(); i++) {
        if (i % 8 == 7) {
            std::cout << str[i] << std::endl;
        } else {
            std::cout << str[i];

        }
    }

}

本站无任何商业行为
个人在线分享 » C++ 字符串分割
E-->