字符串分割

题目描述

连续输入字符串(输出次数为N,字符串长度小于100),请按长度为8拆分每个字符串后输出到新的字符串数组,

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

首先输入一个整数,为要输入的字符串个数。

例如:

输入:
      2

      abc

      12345789

输出:
      abc00000

      12345678

      90000000


输入描述:

首先输入数字n,表示要输入多少个字符串。连续输入字符串(输出次数为N,字符串长度小于100)。



输出描述:

按长度为8拆分每个字符串后输出到新的字符串数组,长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。


输入例子:
2
abc
123456789

输出例子:
abc00000
12345678
90000000

 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 
 5 using namespace std;
 6 
 7 int main(void)
 8 {
 9     int N;
10     string substr;
11     int sublen = 0;
12     while (cin >> N)
13     {
14         string str;
15         while (N--)
16         {
17             cin >> substr;
18             sublen = substr.length();
19             if (sublen % 8 != 0)
20             {
21                 for (int i = sublen % 8; i < 8; i++)
22                 {
23                     substr.push_back('0');
24                 }
25             }
26             else
27             {
28                 for (int i = 0; i < 8; i++)
29                 {
30                     substr.push_back('0');
31                 }
32             }
33 
34             for (int i = 0; i < substr.size(); i++)
35             {
36                 str.push_back(substr[i]);
37             }
38             
39         }
40         for (int i = 0; i < str.size(); i++)
41         {
42             cout << str[i];
43             if ((i + 1) % 8 == 0)
44             {
45                 cout << endl;
46             }
47         }
48         
49     }
50     return 0;
51 }
原文地址:https://www.cnblogs.com/hhboboy/p/5525404.html