Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define MAXLINE 1000    /* max input line size */
  4.  
  5. int getliner(char line[], int maxline);
  6. void copy(char to[], char from[]);
  7.  
  8. /* print longest input line */
  9.  
  10. main()
  11. {
  12.     int len;
  13.     int max;
  14.     char line[MAXLINE];
  15.     char longest[MAXLINE];
  16.    
  17.     max = 0;
  18.     while ((len = getliner(line, MAXLINE)) > 0)
  19.     {
  20.         if(len > max)
  21.         {
  22.             max = len;
  23.             copy(longest, line);
  24.         }
  25.     }
  26.     if(max > 0)
  27.     {
  28.         printf("%s", longest);
  29.     }
  30.     return 0;
  31. }
  32.  
  33. /* getliner: read a line into s, append '\0' to end, return length */
  34.  
  35. int getliner(char s[], int lim)
  36. {
  37.     int c, i;
  38.     for(i = 0; i<lim-1 && (c = getchar()) != EOF && c!='\n'; ++i)
  39.         s[i] = c;
  40.     if(c == '\n')
  41.     {
  42.         s[i] = c;
  43.         ++i;
  44.     }
  45.     s[i] = '\0';
  46.     return i;
  47. }
  48.  
  49. /* copy: copy 'from' into 'to' assume 'to' is big enough */
  50.  
  51. void copy(char to[], char from[])
  52. {
  53.     int i;
  54.     i = 0 ;
  55.     while((to[i] = from[i]) != '\0')
  56.         ++i;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement