Advertisement
khalfella

ansi_c_pg31_ch01_ex16.c

Sep 26th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. /*
  2. * Exercise 1-16.
  3. * Revise the main routine of the longest-line program so it will correctly
  4. * print the length of arbitrary long input lines, and as much as possible of
  5. * the text.
  6. */
  7.  
  8. #include <stdio.h>
  9.  
  10. #define MAXLINE 20 /* maximum input line length */
  11.  
  12. /* getline: read a line into s, return length */
  13. int
  14. get_line(char s[], int lim)
  15. {
  16. int c, i, j;
  17. j = 0;
  18.  
  19. for (i = 0; (c = getchar()) != EOF && c != '\n'; ++i) {
  20. if (i < lim -2) {
  21. s[j++] = c;
  22. }
  23. }
  24. if (c == '\n') {
  25. s[j++] = c;
  26. }
  27. s[j] = '\0';
  28. return (i);
  29. }
  30.  
  31. int
  32. main()
  33. {
  34. int len;
  35. char line[MAXLINE];
  36.  
  37. while ((len = get_line(line, MAXLINE)) > 0) {
  38. printf("TL[%3d] %s", len, line);
  39. }
  40. return (0);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement