•输入一个字符串,请按长度为8拆分每个输入字符串并进行输出;
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
#include <stdio.h> int main() { char ch; char inputStr[102] = {0}; char outputStr[9] = {0}; int count = 0; fgets(inputStr, sizeof(inputStr), stdin); int i = 0; while(inputStr[i] != '\n'){ if(count < 8){ outputStr[count] = inputStr[i]; count++; } if(count == 8){ outputStr[count] = '\0'; printf("%s\n", outputStr); count = 0; } i++; } if(count < 8 && count > 0){ for(;count < 8; count++){ outputStr[count] = '0'; } outputStr[count] = '\0'; printf("%s\n", outputStr); } return 0; }
#include <stdio.h> int main() { char buff[128]; while(scanf("%s",buff) != EOF) { for(int i=0;i<sizeof(buff);i++) { if((i&7)==0 && i!=0)putc('\n',stdout); if(buff[i]) putc(buff[i],stdout); else { if(i&7) for(int j=0;j<8-(i&7);j++) putc('0',stdout); break; } } } return 0; }
#include <stdio.h> #include <string.h> int main() { char str[104]; scanf("%s",str); int len = strlen(str); // printf("len=%d\n",len); if(len==0) { return -1; } int n = len%8; // 字符串处理 if(n!=0) { for(int i=0;i<8-n;i++) { str[len+i] = '0'; } } // printf("str=%s\n",str); // 分割输出 char *p=str; int count=8; while(*p!='\0' && count!=0) { printf("%c",*p++); count--; if(count==0) { printf("\n"); count=8; } } return 0; }
#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; }
#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是对的 然后 网站是错的啊
#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的字符串的时候就报错,为什么呀?
#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; }
#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; }
#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; }
#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; }