题解 | #字符串分隔#
字符串分隔
https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
#include <stdio.h>
#include <string.h>//没有智慧版
int main() {
char str1[101];
fgets(str1,sizeof(str1), stdin);//吸收回车及前所有字符串
int i=0,count=0;
{
int n=strlen(str1);//字符串长度比你看见的还要多一位
if(n<99){//需要考虑满值及接近满值情况
str1[n-1]='\0';//当输出fgets得到的字符串时,最后一位会是回车,需要吸收掉
}
}
for(;i<101;i++){
if(count<8&&str1[i]!='\0'){//确保满8换行且为‘\0’时结束
printf("%c",str1[i]);
count++;//满8计数器
}
else if(str1[i]=='\0'){
for(i=count;i<8;i++){
printf("0");//缺位补0,用计数器与8差值补0
}
break;//需要补0时代表要结束
}
else if(count==8){//让计数器重新计数
printf("\n");
i--;//判断满8时会多进1,需要将这一步减回来
count=0;
}
}
return 0;
}

查看2道真题和解析