题解 | #字符串分隔#
字符串分隔
http://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
利用count记录已经处理的字符数,每8个输出一次,并将count置为0,记得最后检查count是否够8,不够补0
# include<iostream> # include<string> using namespace std; int main() { string str; while(getline(cin, str)) { int len = str.length(); string tmp = ""; int count = 0; for(int i=0;i<len;i++) { tmp+=str[i]; count++; if(count == 8) { cout<<tmp<<endl; tmp = ""; count = 0; } } if(tmp.length()>0&& count<8) { while(count<8) { tmp+='0'; count++; } } cout<<tmp<<endl; } return 0; }