用于打印超过5个字符的所有行的代码

时间:2018-03-10 04:11:20

标签: c

我正在尝试编写应该打印超过5个字符的所有行的代码,而且我不知道代码中的问题所在。你能帮忙吗?

#include <stdio.h>

int main()
{
  int i, s, n ;
  char c, t[100];
  n=0;
  puts("start taping lines");
   while(c=getchar()!=EOF)
   {
   s=0;

   while(c!='/n')
   {

    ++s;
    }
    if (s>=5){
    t[n]=c ;
    ++n;
    }
   }

          puts("lines >80 char =/n");
           for(i=0;i<=n;++i)
           {
               printf("%s /n",t[i]);
           }
  return 0;
}

2 个答案:

答案 0 :(得分:0)

  

假设要打印所有行的代码希望有超过5个字符

而不是将整行保存在t[100]中,这可能太小了,只需保存最多5个字符。遇到至少5个字符然后和所有后续字符时打印它们。

#include <stdio.h>

int main(void) {
  int c;  // Use int here.
  unsigned char buffer[5];

  unsigned n=0;
  puts("start taping lines");

  // This assignment to c is the result of `getchar()!=EOF`  (i.e. 0 to 1)
  // while(c=getchar()!=EOF)

  while((c=getchar()) != EOF) {
    if (n >= 5) {
      putchar(c);
    } else {
      buffer[n++] = c;
      if (n == 5) {
        printf("%.5s",buffer); // print first 5 characters.
      } 
    }
    if (c == '\n') {  // Use `\` not `/`
      n = 0;   
    }
  }
  return 0;
}

答案 1 :(得分:0)

char *line;

if (strlen(line) >= 5)
    printf(line);

在C库中查找strlen()函数。

相关问题