题解 | #字符串分隔#

字符串分隔

https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7

#include <stdio.h>
#include <string.h>
int main() {
    char s[100];//数组1,用来装输入的字符串
    char s2[100];//数组2,用来装输入的字符串以及不足8位补0的0
    int lens;//for循环指数
    int lens2;//for循环指数
    int i;//for循环指数
    int j;//for循环指数
    //输入字符串
    gets(s);
    lens = strlen(s);//计算字符串数组的元素个数

    if(lens%8 !=0){
        lens2 = 8 - lens%8 +lens;//这个将用作新的数组的个数,不足八位的地方补上数差
    }
    else{
        lens2 = lens;
    }
    

    //将数组2不足八位的地方补上字符0
    for(i=0;i<lens2;i++){
       s2[i] = '0';
    }
    //将数组1的元素给到数组2,数组1对应的元素会覆盖到1,数组2没覆盖到的依旧是0
    for(i=0;i<lens;i++){
       s2[i] =  s[i]; 
    }

    //将数组2的元素个数/8得到的整数作为外部大循环,作为输入八个字后换一行
    //输出数组2元素
    for(j=0;j<lens2;j++){
    printf("%c",s2[j]); 
    if((j+1)%8==0 && j!=0){    
    printf("\n");
    }
    
    }

    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务