fork download
  1. #include <stdio.h>
  2.  
  3. #define MAXLINE 40 /* maximum input line size */
  4.  
  5. int getlines(char line[], int maxline);
  6. void copy(char to[], char from[]);
  7.  
  8. /* print longest input line */
  9. int main()
  10. {
  11. int c;
  12. int len; /* current line length */
  13. int max; /* maximum length seen so far */
  14. char line[MAXLINE]; /* current input line */
  15. char longest[MAXLINE]; /* longest line saved here */
  16.  
  17. max = 0;
  18.  
  19. while ((len = getlines(line, MAXLINE)) > 0) {
  20. if (line[len-1] != '\n')
  21. while ((c = getchar()) != EOF && c != '\n')
  22. ++len;
  23.  
  24. if (len > max) {
  25. max = len;
  26. copy(longest, line);
  27. }
  28. }
  29.  
  30. if (max > 0) { /* there was a line */
  31. printf("Longest line with %d characters:\n", max);
  32. printf("%s ...\n", longest);
  33. }
  34.  
  35. return 0;
  36. }
  37.  
  38. /* getline: read a line s, return length */
  39. int getlines(char s[], int lim)
  40. {
  41. int c, i;
  42.  
  43. for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
  44. s[i] = c;
  45. if (c == '\n') {
  46. s[i] = c;
  47. ++i;
  48. }
  49. s[i] = '\0';
  50.  
  51. return i;
  52. }
  53.  
  54. /* copy: copy 'from' into 'to'; assume to is big enough */
  55. void copy(char to[], char from[])
  56. {
  57. int i;
  58.  
  59. i = 0;
  60. while ((to[i] = from[i]) != '\0')
  61. ++i;
  62. }
  63.  
Success #stdin #stdout 0s 2296KB
stdin
asfasd fasdfsadfsadfa
stdout
Longest line with 21 characters:
asfasd fasdfsadfsadfa ...