Advertisement
khalfella

ansi_c_pg31_ch01_ex17.c

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