print-long-lines.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <stdio.h>
  2. #include <string.h>
  3. /* the longest line we will store */
  4. #define MAXLINE 1000
  5. #define MAXPRINTLINE 80
  6. /* print up to the first \n in line */
  7. void
  8. printLine (char * line)
  9. {
  10. /* position */
  11. char * p;
  12. /* find the first occurrence of \n */
  13. p = strchr (line, '\n');
  14. /* while line [j] does not pass the first \n,
  15. putchar (line[j])
  16. */
  17. for (int j = 0; &line [j] != (p + 1); j++)
  18. {
  19. putchar (line [j]);
  20. }
  21. }
  22. int
  23. main ()
  24. {
  25. char c;
  26. /* store each line here */
  27. char line [MAXLINE];
  28. /* loop through every character in the file */
  29. for (int i = 0; (c = getchar()) != EOF; i++)
  30. {
  31. /* store the current character
  32. into the line array */
  33. line [i] = c;
  34. /* if the current char is a newline */
  35. if (c == '\n')
  36. {
  37. /* and the current line is longer than 80 chars */
  38. if (i > MAXPRINTLINE)
  39. {
  40. printLine (line);
  41. }
  42. /* set i back to 0 */
  43. i = -1;
  44. }
  45. }
  46. return 0;
  47. }