首页 > 试题广场 >

字符串分隔

[编程题]字符串分隔
  • 热度指数:1149539 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

•输入一个字符串,请按长度为8拆分每个输入字符串并进行输出;

•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。

输入描述:

连续输入字符串(每个字符串长度小于等于100)



输出描述:

依次输出所有分割后的长度为8的新字符串

示例1

输入

abc

输出

abc00000
#include <stdio.h>
#include <string.h>

int main() {
    char str[9] = "";

    while (1) {
        char tmp[9] = "00000000";
        str[sizeof(str) - 1] = '\0';

        // 最多获取 8 个输入,超过 8 个的输入下次 scanf 获取
        if (scanf("%8s", str) == EOF)
            break;
        
        strncpy(tmp, str, strlen(str));
        printf("%s\n", tmp);
    }
    
    return 0;
}

发表于 2024-04-19 17:37:11 回复(0)
#include <stdio.h>
#include <string.h>

int main() {
    char s[101];
    int j;
    int count = 0;
    scanf("%s", s);
    int len = strlen(s);
    for (int i = 1; i <= len / 8 + 1; i++) {
        count = 0;
        for ( j = 7 * (i - 1); j <= 7 * i; j++) {
            printf("%c", s[j]);
            if (s[j]=='\0') {
                break;
            }
            count++;
        }

        if (count != 8) {
            for (int q = count; q < 8; q++)
                printf("0");
        }


        printf("\n");
    }

    return 0;
}w为啥本地ide是对的  然后 网站是错的啊

发表于 2024-04-02 22:40:36 回复(0)
求大佬们解答问题出在哪里
#include<stdio.h>
#include<string.h>//包含strncpy函数

int Function(char STR[100],char *P,int N);//设置功能递归函数
int main(void)
{
    char str[100];
    char *p=str;
    gets(str);
    int n=strlen(str);//计算字符串的长度
    Function(str,p,n);//调用递归函数
    return 0;
}
int Function(char STR[100],char *P,int N)
{
    if(N>=8)
    {
        int i=1;
        char str2[100];//定义另一个字符串数组用来储存字符串前八位
        strncpy(str2,STR,8);
        puts(str2);//输出这八位
        printf("\n");
        N=N-8;
        for(i=0;i<N;i++)//数组前移八位
        {
            STR[i]=STR[i+8];
        }
        P=STR[N];
        free(P);
        Function(STR,P,N);//递归调用自己
    }
    if(N<8)
    {
        int i=0;
        for(i=N;i<8;i++)
        {
            STR[i]='0';//用0补位
        }
        puts(STR);//输出
        printf("\n");
        return 0;
    }
}

编辑于 2024-03-05 22:40:04 回复(0)
#include <stdio.h>
#include <string.h>

int main(){
    char str[100];
    char str1[8];
    while(scanf("%8s",&str)!=EOF)
    {
        printf("%s\n",str);
        strcpy(str1,str);
        memset(str,'0',8);
        memmove(str,str1,strlen(str1));//如果是100的放在8的头上有问题
        printf("%s\n",str);
    }
}
各位大佬,我这个大于等于8的字符串的时候就报错,为什么呀?


发表于 2024-02-10 20:22:53 回复(0)
#include <stdio.h>
#include <string.h>
int main() {
    char str[101] = {0}, *p;
    fgets(str, 101, stdin);
    if (strlen(str) < 100) {
        str[strlen(str) - 1] = '\0';
    }
    int i = 1;
    p = str;
    while (*p) {
        if (i == 9) {
            printf("\n");
            i = 1;
        }
        printf("%c", *p);
        i++;
        *p++;
    }
    for (i ; i <= 8 ; i++)
        printf("0");

    return 0;
}

发表于 2024-01-11 00:35:35 回复(0)
#include <stdio.h>
#include <string.h>

void spolit(char* s) {
    int len = strlen(s);
    int sum0 = len % 8 == 0 ? 0 : 8 - (len % 8);
    for (int i = 0; i < len + sum0; i++) {
        printf("%c", i < len ? s[i] : '0');
        if ((i + 1) % 8 == 0) {
            printf("\n");
        }
    }
}
int main() {
    char s[101];
    while (scanf("%s", s) != EOF) {
        spolit(s);
    }
    return 0;
}

编辑于 2023-12-21 20:18:54 回复(0)
#include <stdio.h>

int main() {
    char s[108];
    char s1[9];
    gets(s);
    int i = 0,j = 0,m = 0;

    int k =0;
while (s[k]!='\0') {
    k++;
}

    if (s[0] == '\0') {
        return 0;
    }
    m = i+7;
    while(s[m]!='\0'){
        for (j=0; j<8; ) {
            s1[j++] = s[i++] ;
        }
        s1[8] = '\0';
        printf("%s\n",s1);
        m = i+7;
    }
    if (k%8==0) {
        return 0;
    }
    for (j = 0; j<8;) {
            if(s[i]!='\0')s1[j++] = s[i++] ;
            else
            s1[j++] = '0';
        }
    s1[8] = '\0';
    printf("%s",s1);
    return 0;

}

编辑于 2023-12-01 18:13:18 回复(0)
#include<stdio.h>
#include<string.h>

int main()
{
    char str[100] = { 0 };
    int i = 0;
    int j = 0;
    gets(str);
    int len = strlen(str);
    while (len % 8 != 0)
    {
        str[len] = '0';
        len++;
    }
    /*for (int k = 0; k < len; k++)
    {
        printf("%c", str[k]);
    }
    printf("%d", len);*/
    for (i = 0; i < len / 8; i++)
    {
        j = 0;
        for (j = 0; j < 8; j++)
        {
            printf("%c", str[i * 8 + j]);
        }
        printf("\n");
    }
    return 0;
}

发表于 2023-09-14 16:51:16 回复(0)
#include <stdio.h>

int main() {
    int  b,i=7,num;
    char a[100]={0},c='\n';
    scanf("%s",a);
    b=strlen(a);
    if(b<=8){
        for(int j=b;j<8;j++){
            a[j]='0';
       
        }
        printf("%s",a);
    }
    else{
        num=8-b%8+b;//总数
        for(int j=b;j<num;j++){
             a[j]='0';      //填充0  
        }        
     
    for(int i=0;i<num;i++){//打印全部

        printf("%c",a[i]);
        for (int k=1; k<14; k++) {
                if (i==8*k-1) {//输出8个数后加回车
                    printf("%c",c);
                }
        }

    }  

  }

}//为啥只成功26次
发表于 2023-08-20 21:18:37 回复(1)
#include <stdio.h>
#include <string.h>

int main()
{
    int i,l;
    char buff[1000] = {0};
    char buff1[9] = {0};
    gets(buff);
    int length = strlen(buff);
    
    for( i = 0; i < length/8; i ++)
    {
        memset(buff1,0,sizeof(buff1));
        memcpy(buff1,&buff[i * 8],8);
        printf("%s\n", buff1);
    }
    if(length%8)
    {
        memset(buff1,'0',sizeof(buff1));
        memcpy(buff1,&buff[i * 8],length%8);
        
        for(l = 0; l < 8; l++)
        {
            printf("%c", buff1[l]);
        }
        
    }
    return 0;
}
发表于 2023-08-03 23:55:20 回复(0)
/*关键点:先输出第一个字符,当第二个字符下标除以8不等于0,即切断的字符串长度不为8,在字符后一直加0,否则换行‘\0’*/
#include <stdio.h>
#include <string.h>

int main() {
    char s[100];
    int len;
    gets(s);
    len = strlen(s);
    if(len == 0) return 0;
    for(int i=0; i<len;i++){
        printf("%c", s[i]);
        if((i+1)%8 == 0)printf("\n");
        if(s[i+1] == '\0' &&( i+1)%8 != 0){
            for(int j=(i+1)%8; j<8; j++)
                printf("0");
            break;
        }
    }
    return 0;

}

发表于 2023-07-25 07:27:33 回复(0)
//要考虑到最后一个字符是换行符


#include <stdio.h>

int main() {
    char str [108] = {0};
    int i = 0;
    char *p;
    while (scanf("%c", &str[i]) != EOF) { // 注意 while 处理多个 case
        // 64 位输出请用 printf("%lld") to
        i++;
    }
    i = 0;
    int j = 0;
    while(str[i] != 0 && str[i] != '\n')
    {
        for(j = 0; j < 8; j++)
        {
            if(str[i] != 0 && str[i] != '\n')
            {
                printf("%c", str[i]);
                i++;
            }
            else {
                printf("0");
                i++;
            }
        }
        printf("\n");
    }
    return 0;
}

发表于 2023-06-25 22:59:30 回复(0)
#include <stdio.h>
#include"string.h"
int main() {
    char a[101];
    char b[9]={0};
   
    scanf("%[^\n]", &a); 
    char*s =a;
    int x=(strlen(a))/8;
    int y=(strlen(a))%8;
    for(int i=0;i<x;i++)
    {
        memcpy(b,s,8);
        printf("%s\n",b);
        s=s+8;
    }
    
    if(y)
    {
        memset(b,0x30,8);
        memcpy(b,s,y);
        printf("%s",b); 
    }
    return 0;
}
测试存在bug 直接提交能过 无语了
发表于 2023-04-20 22:29:30 回复(0)
/**  题目:HJ4 字符串分隔
  *  思路:每8个字符输出一个 "\n",最后一个字符串缺几个0补几个0
  *  难点:个人认为是如何 缺0补0
  */
#include <stdio.h>
#include <string.h>

int main() {
    char str[101];
    int len;

    gets(str);
    len = strlen(str);

    //  每输出8个字符输出一个"\n"
    for(int i = 0; i < len; i++) {
        printf("%c", str[i]);
        if( (i + 1) % 8 == 0) {
            printf("\n");
        }
    }

    //  最后输出的字符串缺0补0
    for(int j = 0; j < 8 - len % 8; j++) {
        if(len % 8 == 0) {
            break;
        }
        printf("0");
    }

    return 0;
}

发表于 2023-03-26 18:02:46 回复(0)
#include <stdio.h>
#include <string.h>

int main() {
    char str[101];
    int len, i, j, num;
    scanf("%s", str);
    len = strlen(str);

    if (len % 8 == 0) {
        num = len / 8;
    }
    else {
        num = len / 8 + 1;
    }

    for (i = 0; i <= num - 1; i++) {
        for (j = 8 * i; j <= 8 * i + 7; j++) {
            if (j >= len) printf("0");
            else printf("%c", str[j]);
        }
        printf("\n");
    }


    return 0;
}

发表于 2023-03-25 22:16:08 回复(0)

问题信息

难度:
63条回答 135766浏览

热门推荐

通过挑战的用户

查看代码