/*
* Exercise 1-18 Write a program to remove trailing blanks and tabs from
* each line of input, and to delete the entirely blank lines.
*
* fduan, Dec. 11, 2011
*/
#include <stdio.h>
#define MAX_LEN 1000
int getline( char line[], int max_len );
void trim_line( char line[] );
int main()
{
int len;
char line[MAX_LEN] = { '\0' };
while( ( len = getline( line, MAX_LEN ) ) > 0 )
{
trim_line( line );
printf( "%s$\n", line );
}
return 0;
}
int getline( char line[], int max_len )
{
int c, i, j;
i = j = 0;
while( ( c = getchar() ) != EOF && c != '\n' )
{
if( i < max_len - 2 )
line[j++] = c;
++i;
}
if( c == '\n' )
{
line[j++] = '\0'; ++i;
}
return i;
}
void trim_line( char line[] )
{
int j, i = 0;
while( line[i] != '\0' )
++i;
while( i-- > 0 && ( line[i] == ' ' || line[i] == '\t' ) )
line[i] = '\0';
}
K&R C Exercise 1-18 Solution
最新推荐文章于 2012-08-23 20:09:25 发布