Page 31

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

#include <stdio.h>

#define LIMIT 1000

int get_line(char str[], int lim);

int main()
{
	char str[LIMIT];
	int len = 0;

	while ((len = get_line(str, LIMIT)) > 0) {
		if (len > 1) {
			printf("%s", str);
		}
	}

	return 0;
}

int get_line(char str[], int lim)
{
	int c, i = 0;

	while ((c = getchar()) != EOF && c != '\n') {
		if (c != ' ' && c != '\t') {
			if (i < lim - 1) {
				str[i] = c;
			}
			i++;
		}
	}

	if (c == '\n' && i < lim - 1) {
		str[i] = c;
		i++;
	}

	if (i < lim) {
		str[i] = '\0';
	} else {
		str[lim] = '\0';
	}

	return i;
}