Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #define MAXLINE 1000 /* maximum input line length */
  4. int getline(char line[], int maxline);
  5. void copy(char to[], char from[]);
  6. /* print the longest input line */
  7. main()
  8. {
  9.     int len; /* current line length */
  10.     int max; /* maximum length seen so far */
  11.     char line[MAXLINE]; /* current input line */
  12.     char longest[MAXLINE]; /* longest line saved here */
  13.     max = 0;
  14.     while ((len = getline(line, MAXLINE)) > 0)
  15.         if (len > max) {
  16.             max = len;
  17.             copy(longest, line);
  18.         }
  19.     if (max > 0) /* there was a line */
  20.         printf("%s", longest);
  21.     return 0;
  22. }
  23. /* getline: read a line into s, return length */
  24. int getline(char s[],int lim)
  25. {
  26.     int c, i;
  27.     for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
  28.         s[i] = c;
  29.     if (c == '\n') {
  30.         s[i] = c;
  31.         ++i;
  32.     }
  33.     s[i] = '\0';
  34.     return i;
  35. }
  36. /* copy: copy 'from' into 'to'; assume to is big enough */
  37. void copy(char to[], char from[])
  38. {
  39.     int i;
  40.     i = 0;
  41.     while ((to[i] = from[i]) != '\0')
  42.         ++i;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement