samir82show

c_ch01_ex22.c

Oct 27th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. /*Exercise 1-22. Write a program to ``fold'' long input lines into two or more shorter lines after the last non-blank character *that occurs before the n-th column of input. Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column.*/
  2. #include <stdio.h>
  3. #define MAX 1000
  4. char str[MAX];
  5. int fold(int len);
  6. int main()
  7. {
  8. int len;
  9. while ((len = fold(MAX)) > 0)
  10. printf("%s\n", str);
  11. return 0;
  12. }
  13. int fold(int len) {
  14. int c, i, lc;
  15. i = lc = 0;
  16. while ((c = getchar()) != EOF && c != '\n' && i < len - 1 ) {
  17. lc++;
  18. if (lc >= 40) {
  19. if (c != ' ' && c != '.' && c != ',' && c != ';' && c != '(')
  20. str[i++] = '-';
  21. str[i++] = '\n';
  22. lc = 0;
  23. }
  24. str[i++] = c;
  25. }
  26. str[i] = '\0';
  27. return i;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment