题解 | #字符串分隔#
字符串分隔
https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
#include <iostream>
#include <string>
#include <vector>
using namespace std;
/*输入一个字符串,请按长度为8拆分每个输入字符串并进行输出;
长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。*/
//连续输入字符串(每个字符串长度小于等于100)
//思路:1.使用字符串分割函数substr 每隔8个字符进行分割,该函数不会影响本字符串,需要重新复制到操作的字符串中
// 2.针对不够8位的 直接使用+运算符 直到长度为8
int main()
{
string str;
getline(cin, str);
int len = str.length();
while (len>=8)
{
cout << str.substr(0, 8) << endl;
str = str.substr(8);
len -= 8;
}
if (len != 0)
{
//先找到还差多少长度 再进行补0
//或者循环加0 直到长度为8
while (str.size() != 8)
{
str += "0";//字符串可以直接+ 有+重载运算符
}
cout << str << endl;
}
system("pause");
return 0;
}