My method, it seems easy, because I read a line a time!
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 MAXLINE 1000 /* maximum input line length */
int getline(char line[], int maxline);
int trail_line(char line[],int length);
int main(void)
{
int len; /* current line length */
char line[MAXLINE]; /* current input line */
while ((len = getline(line,MAXLINE)) > 0)/*actuall length or the line,contain '/n'*/
{
printf("%s:%d/n",line,len);
/*At least more than a '/n' exit!*/
if(len > 1)
len = trail_line(line,len);
/*At least more than a '/n' exit after trailing!*/
if(len > 1)
printf("%s:%d",line,len);
}
return 0;
}
/* getline: read a line into s, return length */
int getline(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';// Notice it is needed!
return i;
}
int trail_line(char line[], int length)
{
int i;
i = length-2;
while(i >= 0)
{
if(line[i] == ' ' || line[i]== '/t')
i--;
else
{
line[++i] = '/n';
line[++i] = '/0';
break;
}
}
return i;
}
Method 2 in C Answer Book
#include <stdio.h>
#include <stdlib.h>
#define MAXQUEUE 1001
int advance(int pointer)
{
if (pointer < MAXQUEUE - 1)
return pointer + 1;
else
return 0;
}
int main(void)
{
char blank[MAXQUEUE];/*it is used to record the blank chars, is a queue*/
int head, tail;
int nonspace;
int retval;
int c;
retval = nonspace = head = tail = 0;
/*Read a char a time*/
while ((c = getchar()) != EOF)
{
if (c == '/n')
{
head = tail = 0;
if (nonspace)/*not an entirely blank line*/
putchar('/n');
nonspace = 0;
}
else if (c == ' ' || c == '/t')
{
if (advance(head) == tail) /*whether the consecutively occured blank chars overflow*/
{
putchar(blank[tail]);
tail = advance(tail);
nonspace = 1;
retval = EXIT_FAILURE;
}
blank[head] = c;
head = advance(head);
}
else
{
while (head != tail)
{
putchar(blank[tail]);/*print blank value first*/
tail = advance(tail);
}
putchar(c);/*print the non-blank char secondly*/
nonspace = 1;
}
}
return retval;
}
Method3: fix a bug in method 2
if there is a huge block of non-trailing whitespace (eg "A",2000
spaces, "B/n") method1 returns an error when there's not a need for
it.
#include <stdio.h>
#include <stdlib.h>
#define MAXQUEUE 1001
int advance(int pointer)
{
if (pointer < MAXQUEUE - 1)
return pointer + 1;
else
return 0;
}
int main(void)
{
char blank[MAXQUEUE];
int head, tail;
int nonspace;
int retval;
int c;
int spaceJustPrinted; /*boolean: was the last character printed whitespace?*/
retval = spaceJustPrinted = nonspace = head = tail = 0;
while ((c = getchar()) != EOF) {
if (c == '/n') {
head = tail = 0;
if (spaceJustPrinted == 1) /*if some trailing whitespace was printed...*/
retval = EXIT_FAILURE;
if (nonspace) {
putchar('/n');
spaceJustPrinted = 0; /* this instruction isn't really necessary since
spaceJustPrinted is only used to determine the
return value, but we'll keep this boolean
truthful */
nonspace = 0; /* moved inside conditional just to save a needless
assignment */
}
}
else if (c == ' ' || c == '/t') {
if (advance(head) == tail) {
putchar(blank[tail]); /* these whitespace chars being printed early
are only a problem if they are trailing,
which we'll check when we hit a /n or EOF */
spaceJustPrinted = 1;
tail = advance(tail);
nonspace = 1;
}
blank[head] = c;
head = advance(head);
}
else {
while (head != tail) {
putchar(blank[tail]);
tail = advance(tail);
}
putchar(c);
spaceJustPrinted = 0;
nonspace = 1;
}
}
/* if the last line wasn't ended with a newline before the EOF,
we'll need to figure out if trailing space was printed here */
if (spaceJustPrinted == 1) /*if some trailing whitespace was printed...*/
retval = EXIT_FAILURE;
return retval;
}