Exercise 1-18 (Trim file - Blanks, tabs, empty lines; getline)
Chapter_1 Exercise_1-17 | Exercise_1-19 |
Exercise 1-18 K&R, p. 31
Exercise 1-18. Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines.
CONTENTS: trimfile1.c trimfile2.c
Note: In trimfile2.c we also remove beginning whitespace (blanks and tabs) from each line of input.
trimfile1.c download
#include <stdio.h> // for getchar(), printf(), EOF
#define MAXLINE 1000 // maximum input line length
int getLine(char line[], int maxline); // getline() is declared in stdio.h
int trimline(char line[], int length);
// remove trailing blanks and tabs, delete blank lines
int main()
{
int len; // current line length
char line[MAXLINE]; // current input line
// line containing only '\n' has length 1:
while((len = getLine(line, MAXLINE)) > 0)
{
if (trimline(line, len) > 0) // nonempty line
{printf("%s\n", line);} // trimmed line doesn't end with '\n'
}
return 0;
}
// getLine(): read a line into s[] up to MAXLINE-1 chars, return length
int getLine(char s[], int lim)
{
int c = EOF; // initialize
int i;
// getchar() is only executed if (i < (lim-1)):
for (i = 0; i < (lim-1) && (c = getchar()) != EOF && c != '\n'; i++)
{ // from 0 to lim-2; s[lim-2]='\n', s[lim-1]='\0'
s[i] = c;
}
if (c == '\n') // i < (lim-1), getchar() executed
{
s[i] = c; // '\n'
i++;
}
s[i] = '\0'; // the null character ends a string
return i; // max(i) == (lim-1)
}
// remove trailing blanks, tabs, ending '\n', return new length
int trimline(char line[], int len)
{
int pos; // last nonempty position
pos = len-1; // last char before ending '\0'
if (line[pos] == '\n')
{
line[pos] = '\0'; // remove ending '\n'
pos--;
}
while (pos >= 0)
{
if (line[pos] == ' ' || line[pos] == '\t')
{
line[pos] = '\0';
pos--;
}
else break; // out of while()
}
if (pos < 0) {return 0;} // blank line
// else return (pos+1);
return (pos+1); // new length
}
/*
gcc trimfile1.c -o trimfile1
./trimfile1 // input from the keyboard
./trimfile1 < trimfile1.c // source file
./trimfile1 < trimfile1 // binary file
*/
trimfile2.c download
#include <stdio.h> // for getchar(), printf(), EOF
#define MAXLINE 1000 // maximum input line length
int getLine(char line[], int maxline); // getline() is declared in stdio.h
int trimline(char line[], int length);
// remove beginning and trailing blanks and tabs, delete blank lines
int main()
{
int len; // current line length
char line[MAXLINE]; // current input line
// line containing only '\n' has length 1:
while((len = getLine(line, MAXLINE)) > 0)
{
if (trimline(line, len) > 0) // nonempty line
{printf("%s\n", line);} // trimmed line doesn't end with '\n'
}
return 0;
}
// getLine(): read a line into s[] up to MAXLINE-1 chars, return length
int getLine(char s[], int lim)
{
int c = EOF; // initialize
int i;
// getchar() is only executed if (i < (lim-1)):
for (i = 0; i < (lim-1) && (c = getchar()) != EOF && c != '\n'; i++)
{ // from 0 to lim-2; s[lim-2]='\n', s[lim-1]='\0'
s[i] = c;
}
if (c == '\n') // i < (lim-1), getchar() executed
{
s[i] = c; // '\n'
i++;
}
s[i] = '\0'; // the null character ends a string
return i; // max(i) == (lim-1)
}
// remove beginning and ending whitespace, return new length
int trimline(char line[], int length) // length > 0
{
int i = 0, j, index;
while (i < length && (line[i] == ' ' || line[i] == '\t'))
{i++;}
if (i == length || line[i] == '\n')
{
line[0] = '\0'; // empty trimmed line
return 0;
}
// from here on non-empty trimmed line (from the beginning)
if (line[length-1] == '\n')
{j = length-2;} // remove ending '\n'
else {j = length-1;}
if (j >= i) // i >= 0
{
while (line[j] == ' ' || line[j] == '\t')
{j--;}
}
if (i > 0) // shift line to the beginning (remove beginning whitespace)
{
for (index = 0; index < j-i+1; index++)
{line[index] = line[index+i];}
}
line[j-i+1] = '\0'; // end line before trailing whitespace
return j-i+1; // new length
}
/*
gcc trimfile2.c -o trimfile2
./trimfile2 // input from the keyboard
Hello!
Hello!
Have a nice day!
Have a nice day!
// Ctrl^D in Linux, Ctrl^Z+Enter in Windows (EOF)
./trimfile2 < trimfile2.c // input from source file
./trimfile2 < trimfile2 // binary file
*/
Chapter_1 Exercise_1-17 | BACK_TO_TOP | Exercise_1-19 |
Comments
Post a Comment