samir82show

c_ch01_ex18.c

Oct 10th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. /*Exercise 1-18. Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines.*/
  2. #include <stdio.h>
  3. #define MAX 1000
  4. int untrail(char str[], int length);
  5. int main()
  6. {
  7. int length;
  8. length = 0;
  9. char str[MAX];
  10. while ((length = untrail (str, MAX)) > 0)
  11. printf ("%s\n", str);
  12. return 0;
  13. }
  14. int untrail(char str[], int length) {
  15. int c, i, j, flag;
  16. i = j = flag = 0;
  17. while ((c = getchar()) != EOF && c != '\n' && i < length - 1) {
  18. if (c == ' ' || c == '\t') {
  19. flag = 1;
  20. j += 1;
  21. } else {
  22. flag = 0;
  23. j = 0;
  24. }
  25. str[i++] = c;
  26. }
  27. if (flag) {
  28. str[i - j] = '\0';
  29. return j;
  30. } else {
  31. str[i] = '\0';
  32. return i;
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment