Guest User

Untitled

a guest
May 21st, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. // This version of printLongestLine prints the longest input line, but is specialized to use external variables. These are external to all functions, and can be accessed by name by any function.
  2.  
  3. #include <stdio.h>
  4.  
  5. #define MAXLINE 1000 //maximum input line size
  6.  
  7. int max; // maximum input line size
  8. char line[MAXLINE]; // current input line
  9. char longest[MAXLINE]; // longest line saved here
  10.  
  11. int mygetline(void);
  12. void copy(void);
  13.  
  14. // print longest input line;, specialized version
  15.  
  16. main()
  17. {
  18. int len;
  19. extern int max;
  20. extern char longest[];
  21.  
  22. max = 0;
  23. while ((len = mygetline()) > 0) {
  24. if (len > max) {
  25. max = len;
  26. copy();
  27. }
  28.  
  29. }
  30. if (max > 0)
  31. printf("%s", longest);
  32. return 0;
  33. }
  34.  
  35. // mygetline: specialized version
  36.  
  37. int mygetline(void)
  38. {
  39. int c, i;
  40. extern char line[];
  41.  
  42. for (i = 0; i < MAXLINE -1 && (c = getchar()) != EOF && c != '\n'; ++i)
  43. line[i] = c;
  44. if (c == '\n') {
  45. line[i] = c;
  46. ++i;
  47. }
  48. line[i] = '\0';
  49. return i;
  50. }
  51.  
  52. // copy: specialized version
  53.  
  54. void copy()
  55. {
  56. int i;
  57. extern char line[], longest[];
  58.  
  59. i = 0;
  60. while (longest[i] = line[i] != '\0' )
  61. ++i;
  62. }
Add Comment
Please, Sign In to add comment