Kernigan-Richie mission



  • Please tell me you have a small buffer and a larger streaming line than a buffer. In an endless cycle, you're buffering the line until you meet the "n" symbol, so we've learned its length. Then I'd like to consider it a new buffer with a suitable size. How do you get the carriage back to the beginning of the line? I sold putchar. Target 1.16

    int len;                // Длина текущей строки
    int max;                // Длина максимальной строки из просмотренных
    int current;            // Длина текущей строки
    char line[MAXLINE];     // Текущая строка
    char longest[MAXLINE];  // Максимальная из предыдущих строк
    

    max = 0;
    current = 0;

    while ((len = getline(line, MAXLINE)) > 0)
    {
    current +=
    len; // Если считанная строка больше MAXLINE, то выделить необходимый размер

    if (line[len - 1] == '\n')
    {
        putchar('\r');
        getline(line, current);
        max = current;
        copy(longest, line);
    }
    

    }

    if (max > 0) //Была ли хоть одна строка?
    printf("%s \n", longest); //Печать самой длинной строки

    return 0;

    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;
    }
    
    s[i] = '\0';
    return i;
    

    }

    // copy - копирует строку из 'from' в 'to', 'to' - достаточно большой

    void copy(char to[], char from[])
    {
    int i = 0;

    while ((to[i] = from[i]) != '\0')
        ++i;
    

    }



  • In your formulation, without the dynamic distribution of memory, the task is not resolved in principle. But the point is, in the original, the task is different. Comparison, Russian translation:

    Operation 1.16. Rewrite the main previous program so that it can Could have printed the longest line without any restrictions on it. size.

    And the original:

    Exercise 1-16. Revise the main routine of the longest-line program so it will correctly print the length of arbitrary long input lines, and as much as possible of the text.

    That is the wording that is already being addressed without problems.

    Conclusion: Always check the correction input data, and not only in the software code 🙂


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2