c语言求助
请大家帮忙,谢谢。
A html file is a text file with embedded tags in it.
The tags describe sections and formating information.
Tags often appear in pairs, with the text they format in between.
For example, <font color=red> Red Heading </font>.
The <br> tag appears on its own and indicates a new line.
We want a way to convert from html files to text files that replaces <br>
with a new line character and removes all the tags.
You can assume the characters < and > are only used in tags.
You can also assume that tags always contain lower case characters.
Write a solution to the problem using C programs that read from
standard input and write to standard output.
The first program (debr) reads the input data and pipes it to the next
program (detag) unchanged except that all sequences '<', 'b', 'r', '>'
generate an extra new line character. The detag program outputs the
text except for the tags, that is '<' and everything including the
next '>' are removed.
Here is an example of how it works:
turing % cat test.html
html>
<title> This Page </title>
<body>
Some text on the page
<br> <br>
<font size=9> Big text </font>
</body>
</html>
turing % gcc debr.c -o debr
turing % gcc detag.c -o detag
turing % cat test.html |debr |detag
This Page
Some text on the page
Big text
Please submit your two C source code programs and record a script of the
programs being compiled and then running on some test file.
参考答案:可以这样写:
/* debr.c */
#include <stdio.h>
void main( ) {
char buf[ 5 ] = { 0 };
while ( ( buf[ 0 ] = getchar( ) ) != EOF )
if ( buf[ 0 ] == '<' ) {
scanf( "%3c", buf + 1 );
printf( "%s", buf );
if ( buf[ 1 ] == 'b' && buf[ 2 ] == 'r' && buf[ 3 ] == '>' )
puts( "" );
}
else
putchar( buf[ 0 ] );
}
################################################################################
################################################################################
/* detag.c */
#include <stdio.h>
void main( ) {
char c;
while ( ( c = getchar( ) ) != EOF )
if ( c == '<' )
while ( ( c = getchar( ) ) != '>' )
;
else
putchar( c );
}