K&R Exercise 1.16 - Limitation on line length

假如想象 提交于 2019-11-30 08:34:13

It's a pretty early exercise in K&R, you're just supposed to do some minor changes to the code, not a total redesign of the code.

  1. "...as much as possible of the text..."

    is up to you to interpret. I'd do it by printing what's stored in the longest buffer. i.e. print out up to 1000 characters of the line. Again, it's an early exercise, with little introduction to dynamically allocated memory yet. And at the time K&R was written, storing away arbitrarily long text lines wasn't as feasible as it is today.

  2. "...the length of arbitrarily long input lines..."

    Is a hard requirement. You're supposed to find the correct length no matter how long it is (at least within the bounds of an int. )

One way to solve this problem is:

  • After the call to getline(), check if the last character read into the line buffer is a newline ('\n')
  • If it is, you read a complete line. The len variable is the correct length of the line(the return value of getline(), and no special consideration is needed compared to to original code.
  • If it is not , you did not read the entire line, and need to hunt for the end of this line. You add a while loop, calling getchar() until it returns a newline (or EOF), and count the number of characters you read in that loop. Just do len++ to count.
  • When the while loop is done, the new len is now the actual length of the line, but our buffer just has the first 999 characters of it.
  • As before, you store away (the copy() function call) the current line buffer (max 1000 chars) if this line is the longest so far.
  • When you're done, you print out the stored line as before (the longest buffer) and the max variable for the length.
    • Due to the above mentioned while loop that max length is now correct.
    • If the longest line indeed was longer than 1000 chars. you at least print out those first 999 chars - which is "as much as possible".

I'll not spoil it and post the code you need to accomplish this, but it is just 6 lines of code that you need to add to the longest-line program of exercise 1-16.

  1. On modern machines "as much as possible of the text" is likely to be all of the text, thanks to automatically line-wrapping terminal programs. That book was written when teletype terminals were still in use. There is no limitation on string length other than perhaps memory limitations of the machine you're working on.

  2. They're expecting you to add some kind of loop to read characters and look for newlines rather than assuming that a read into the MAXLINE sized buffer is going to contain a newline for sure.

here is my version:

int getline(char s[],int lim)
{
    int c,i;
    for(i=0;i<lim-1&&(c=getchar())!=EOF&&c!='\n';++i)
        s[i]=c;
    if(c=='\n')
    {
        s[i]=c;
        ++i;
    }
    if(c!=EOF)
    {
        while((c=getchar())!=EOF&&c!='\n')
            i++;
    }
    s[i]='\0';
    return i;
}
    #define MAXLINE 1000
    int len;
    int max;
    char line[MAXLINE];
    char longest[MAXLINE];

    max=0;
    while((len=getline(line,MAXLINE))>1)
    {
        if(len>max)
        {
            max=len;
            copy(longest,line);
        }
    }
    if(max>0)
    {
        printf("%d:%s",max,longest);
    }
    return 0;

for some unknown reasons ,the example code doesn't work in my pc particularly,when the condition is 'len>0',the loop won't end i think the main reason is that when you type nothing,but you still have to press enter,so it is received as '\n',and the len is 1; i think it satisfy the requirement that print the length of arbitrarily long input lines, and as much as possible of the text. And it works like this



    #include

    main()
    {
       long tlength = 0;
       short input, llength = 1;
       while (llength > 0)  {
          llength = 0;
          while ((input = getchar()) != EOF) {
              ++llength;
              if (input == '\n')
              break;
          }
          tlength = tlength + llength;
          printf("\nLength of just above line : %5d\n\n", llength);
       }
       printf("\n\tLength of entire text : %8ld\n", tlength);
       return 0;
    }

According to me, This question only wants the length of each arbitrarily line + At last the length of entire text.

Try to run this code and tell me is it correct according to question because i too confuse in this problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!