题解 | #字符串分隔#
字符串分隔
https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
char strout[8];
while(gets(str))
{
int len = strlen(str); //字符串长度
int x = 8 - (len % 8); //末尾加零的个数
for(int i = 0; (i < len/8 ); i++){
strncpy(strout, str + 8*i, 8); //strncpy(str1, str2 + m, n); 从str2中第str[m]开始的n个字符复制到str1
strout[8] ='\0'; //strncpy复制不包括'\0',需要再手动添加一个'\0'
printf("%s\n",strout);
}
strncpy(strout, str + len - (len % 8), (len % 8)); //输出最后几位
for(int i = 0; (i < x); i++) //加0
strout[(len % 8) + i] = '0';
strout[8] ='\0';
if(x != 8)
printf("%s\n",strout);
}
}