weixin_34433129
KingintheNorth
2016-04-30 08:13
采纳率: 87.5%
浏览 1.4k

C语言请大神帮忙找BUG

#include
#define MAXIMUM 1000 //这段代码是要实现将一个字符串中的连续的空格用数
int getline(char line[]); //较少的指标符和空格代替,请指点,如其中有什么低等
main(){ //错误也请大神们指点,本人新手只懂皮毛,谢谢。
int len;
char line[MAXIMUM];
char newline[MAXIMUM];
int j;
int k = 0;
int ecount;
int n;
while((len = getline(line) > 0)){
for(j = 0;j < len;j++){
if(line[j] != ' ')
newline[k++] = line[j];
else if(line[j] == ' '){
while(line[j++] == ' '){
ecount++;
}
if(ecount == 1){
newline[k++] = ' ';
}else if(ecount == 2){
newline[k++] = ' ';
newline[k++] = ' ';
}else if(ecount >= 3){
for(n = 0;n <(ecount - (ecount%3))/3;n++){
newline[k++] = '\t';
}
for(n = 0;n < (ecount%3);n++){
newline[k++] = ' ';
}
}
newline[k++] = line[j];

}
}
}
printf("%s\n",newline);
return 0;
}
int getline(char line[]){
int i;
int c;
for(i = 0;(c = getchar()) != EOF&&c != '\n';i++)
line[i] = c;
if(c = '\n'){
line[i] = c;
i++;
}
line[i] = '\0';
return i;
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • pengyue_gg
    pengyue_gg 2016-05-02 07:47
    已采纳

    错误太多,排版啦、== 用错成 = 啦、循环计数各种乱啦,回去再学学吧

     #include <stdio.h>
    #define MAXIMUM 1000 
    int getline(char * line); 
    int main(){ 
      int len;
      char line[MAXIMUM];
      char newline[MAXIMUM];
    
      while((len = getline(line)) > 0){
        int j = 0;
        int k = 0;
    
        printf("src[%d]:%s\n", len, line);
        for(j = 0; j < len; ){
          if(line[j] != ' ') newline[k++] = line[j++];
          else {
            int ecount = 0;
            int i = j;
            while(line[i++] == ' ')ecount++;
            j += ecount;
            int tab_count = ecount / 3; 
            int space_count = ecount - tab_count * 3;
    
            int n;
    
            //你用tab和空格调试的时候能看见吗???
            for(n = 0; n < tab_count; n++)newline[k++] = '#';     
            for(n = 0; n < space_count; n++)newline[k++] = '_';
          }
    
        }
      //newline[len] = '\0';
      printf("k:%d %s\n",k, newline);
      }
    
      return 0;
    }
    int getline(char * line){
      int i, c;
      for(i = 0; (c = getchar()) != EOF && c != '\n'; i++) line[i] = c;
      if(line[0] == '\0')return 0;
      line[i] = '\0';
      return i;
    }
    
    点赞 评论
  • caozhy

    什么是“数较少的指标符和空格”

    点赞 评论
  • u012155923
    咖啡不加盐 2016-04-30 23:19

    可以把代码发我一份550188053@qq.com,帮你debug

    点赞 评论

相关推荐