简体   繁体   中英

My program can't output the lines correctly

I'm relatively new in C and I currently reading Kernighan's book. One of the problems in the book is to create an algorithm that from a input line output the line if it is more than 10 characters long.

The point is I'm frustrated because I cant find what is wrong with my code. I debugged and recreate it many times but still cant find out what's going on!

The escape character from function getl() is '.' (dot), and sometimes works and other times don't. If you compile it and test you will see:

gcc -Wall -o out 'script.c'

The question header from the book is:

“Exercise 1-17. Write a program to print all input lines that are longer than 10 characters.”

I'm sure that's relatively easy, but I really wanted to know why this algorithm is not working as expected, i think it has something to do with '\\n'.

If someone could help me find out what's the problem with the code, I would appreciate it.

Code

#include <stdio.h>

#define MAX 10000

int getl(char line[], int lim) {
    char c;
    int count;

    for (count = 0 ; count < lim-1 && (c = getchar()) != '.' ; count++) {
        if (c == '\n') {
            line[count] = '\n';
            count++;
            break;
        }
        line[count] = c;
    }
    line[count] = '\0';
    return count;
}
    
int main() {
    char line[MAX];
    int len = 1;

    for (; len > 0 ;) {
        getl(line, MAX);
        len = getl(line, MAX);
        if (len >= 10)
            printf("%s", line);
    }
    return 0;
}

Your code almost works. You just seem to have some repeated lines here and there that confuse things.

Specifically, you are calling getl(line, MAX); twice in a row. The first gets the input, but don't save the count, the second has only an empty stdin buffer to work with so no sensible count is saved from that. Removing the first call that don't save the count fixes your issue.

#include <stdio.h>

#define MAX 10000

int getl(char line[], int lim) {
    char c = getchar();
    int count;
    
    for (count = 0 ; c != '.' ; count++) {
        line[count] = c;
        c = getchar();
    }
    line[count++] = '\n';
    return count;
}
    
int main() {
    char line[MAX];
    int len = 1;
    for (; len > 0 ;) {
        len = getl(line, MAX);
        if (len >= 10)
            printf("%s", line);
    }
    return 0;
}

First, you're calling your getl function twice instead of once (you only want to read lines one by one). Fixing that should work. Then I think you shouldn't add the trailing '\\n' to your lines, just print it when your line is longer than 10 characters, in your code, the '\\n' will be counted as a character.

Here's the modified code:

#include <stdlib.h>
#include <stdio.h>
#define MAX 10000

int getl(char line[])
{
    char    c;
    int     count;

    for (count = 0; count < MAX - 1 && (c = getchar()) != '.' ; count++)
    {
        if (c == '\n')
            break;
        line[count] = c;
    }
    line[count] = '\0';
    return (count);
}

int main()
{
    char    line[MAX];
    int     len = 1;

    while (len > 0)
    {
        len = getl(line);
        if (len >= 10)
            printf("%s, c = %i\n", line, len);
    }
    return (0);
}

This should work. https://ideone.com/cXXRUH

#include <stdio.h>

#define MAX 10000

int getline_length(char line[]) {
  char ch;
  int count = 0;

  printf("\nWaiting for INPUT...");
  // Using clear while loop to get input, removing redundent complexity 
  // Either `.` or `\n` consider End Of Line 
  while(count < MAX-1 && ((ch = getchar()) != '.' || (ch = getchar()) != '\n')) {
      line[count++]=ch;
  }
  line[count] = '\0';
  return count;
}

int main() {
   char line[MAX];

   while(1) {
       // reset array before each input 
       memset(line, 0, sizeof(line));
       int len = getline_length(line); //No need to pass limit 
       if (len >= 10) {
           printf("%s", line);
        } else {
            printf("len < 10");
        }
   }
   return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM