首页 > 其他 > 详细

字符串分割

时间:2016-05-25 00:26:20      阅读:250      评论:0      收藏:0      [点我收藏+]

题目描述

连续输入字符串(输出次数为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 }

 

字符串分割

原文:http://www.cnblogs.com/hhboboy/p/5525404.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!