{"content":{"id":557,"title":"K\u0026R Solutions - 1.18","body":"\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines.\u003c/p\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e\u003c/p\u003e \r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e#include \u0026ltstdio.h\u0026gt\r\n#define MAXLINE 1000\r\n\r\nint get_a_line(char s[], int lim);\r\nvoid chop(char s[], int lim);\r\n\r\nint main(void)\r\n{\r\n int len;\r\n char line[MAXLINE];\r\n\r\n while ((len = get_a_line(line, MAXLINE)) \u003e 0) {\r\n if (line[0] == '\\n') {\r\n // Nothing happens. We want to totally ignore strings that are just\r\n // newlines.\r\n } else {\r\n chop(line, MAXLINE);\r\n printf(\"%s\u003c---End Of String\\n\", line);\r\n }\r\n }\r\n\r\n return 0;\r\n}\r\n\r\n// Same as the previous exercise.\r\nint get_a_line(char s[], int lim) \r\n{\r\n int c, i;\r\n\r\n for (i = 0; i \u003c lim-1 \u0026\u0026 (c=getchar()) != EOF \u0026\u0026 c != '\\n'; ++i) {\r\n s[i] = c;\r\n }\r\n if (c == '\\n') {\r\n s[i] = c;\r\n ++i;\r\n }\r\n s[i] = '\\0';\r\n \r\n return i;\r\n}\r\n\r\nvoid chop(char s[], int lim) {\r\n int i;\r\n\r\n // First we find the end of the string.\r\n // Notice that the the resulting value of 'i' is the length of the string.\r\n // Also notice that in this context, we could have just moved to the newline\r\n // before the null char.\r\n for (i = 0; s[i] != '\\0'; ++i) {\r\n }\r\n\r\n // Now move backwards again, untill we find something that's not whitespace.\r\n for (i -= 1; s[i] == '\\t' || s[i] == '\\n' || s[i] == ' '; --i) {\r\n }\r\n\r\n // And we replace whatever is after with a null char.\r\n // We're replacing the newline, even though the exercise doesn't tell us\r\n // to get rid of it. This is becuse it makes the output easier to test.\r\n s[i+1] = '\\0';\r\n}\r\n\u003c/code\u003e\u003c/pre\u003e\r\n\u003cpre\u003e\u003ccode class=\"lisp\"\u003e./trimmer\r\nhi \t (enter)\r\nhi\u003c---End Of String\r\nhow are you?\t\t(enter)\t \r\nhow are \tyou?\u003c---End Of String\r\n(enter)\r\nthree little birds(enter)\r\nthree little birds\u003c---End Of String\r\n\u003c/code\u003e\u003c/pre\u003e","publication_date":"2019-03-20T00:00:00.000Z","created_at":"2019-02-18T20:55:42.000Z","updated_at":"2019-02-18T22:31:22.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=k%26r\"\u003ek\u0026amp;r\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=unix\"\u003eunix\u003c/a\u003e"}

K&R Solutions - 1.18

Q: Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines.

A:

#include <stdio.h>
#define MAXLINE 1000

int get_a_line(char s[], int lim);
void chop(char s[], int lim);

int main(void)
{
  int len;
  char line[MAXLINE];

  while ((len = get_a_line(line, MAXLINE)) > 0) {
    if (line[0] == '\n') {
      // Nothing happens. We want to totally ignore strings that are just
      // newlines.
    } else {
      chop(line, MAXLINE);
      printf("%s<---End Of String\n", line);
    }
  }

  return 0;
}

// Same as the previous exercise.
int get_a_line(char s[], int lim) 
{
  int c, i;

  for (i = 0; i < lim-1 && (c=getchar()) != EOF && c != '\n'; ++i) {
    s[i] = c;
  }
  if (c == '\n') {
    s[i] = c;
    ++i;
  }
  s[i] = '\0';
  
  return i;
}

void chop(char s[], int lim) {
  int i;

  // First we find the end of the string.
  // Notice that the the resulting value of 'i' is the length of the string.
  // Also notice that in this context, we could have just moved to the newline
  // before the null char.
  for (i = 0; s[i] != '\0'; ++i) {
  }

  // Now move backwards again, untill we find something that's not whitespace.
  for (i -= 1; s[i] == '\t' || s[i] == '\n' || s[i] == ' '; --i) {
  }

  // And we replace whatever is after with a null char.
  // We're replacing the newline, even though the exercise doesn't tell us
  // to get rid of it. This is becuse it makes the output easier to test.
  s[i+1] = '\0';
}
./trimmer
hi 	  (enter)
hi<---End Of String
how are         you?		(enter)	 
how are 	you?<---End Of String
(enter)
three little birds(enter)
three little birds<---End Of String