K&R Exercise 1.16 - Limitation on line length

前端 未结 5 607
时光说笑
时光说笑 2020-12-31 04:17

I\'m learning C from K&R\'s \"The C Programming Language\" book. I\'m doing the exercises specified in the book. I\'m on exercise number 1.16, but I don\'t understand it

5条回答
  •  青春惊慌失措
    2020-12-31 04:57

    here is my version:

    int getline(char s[],int lim)
    {
        int c,i;
        for(i=0;i1)
        {
            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

提交回复
热议问题