Write a C program which reads the standard input and extract
Write a C program which reads the standard input and extract HTML-style tags from it. We consider that the HTML-style tags are all strings starting with character “less-than” (<) and end with the character “greater-than” (>). In case that there is a tag starting with ‘<’ but not finishing with ‘>’ at all, then print it all the way to the end of input. Each tag should start at a new line. Input The input is in a free textual form. Output The output consists of a list of tags in the order that they appear in text. Each tag should start on a new line. All content of a tag should be printed in the same way as it appears in input. If a tag is not finished, it should still be printed. The sample input and output below illustrate behaviour of the program.
Hint : The program should read input character by character and immediatelly produce output or not based on the context.
Hint : you can use getchar() and putchar() OR scanf() and prinf()
Sample Input:
Sample Output:
Solution
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char data[50][50];
int c, nr = 0, nc = 0, t = 0;
char temp[600];
//Asks for number or row data to be inputted
strcpy(temp, \"\");
printf(\"\ Enter the data: \");
do
{
fflush(stdin);
//%[^\ ]s for new line character (\ )
scanf(\"%[^\ ]s\", data[nr]);
t = strlen(data[nr]);
strcat(temp, data[nr]);
strcat(temp, \" \");
if(data[nr][t-1] == \'.\')
break;
nr++;
}while(1);
nc = strlen(temp);
printf(\"\ Enter data: \ \");
for(c = 0; c < nc; c++)
{
//if it is a < symbol or / symbol
if(temp[c] == \'<\' || temp[c+1] == \'/\' )
{
//Loops till > character
do
{
fflush(stdout);
putchar(temp[c]);
c++;
}while(temp[c] != \'>\' && temp[c-1]!= \'.\');
//Adds a > symbol because it is excluded in the above
putchar(\'>\');
printf(\"\ \");
}
}
}
Output:
Enter the data: This is sample intermediate text <a tag> and </another tag>
another example. <a tag may contain < character > and > character
may appear outside of a tag. <a tag can
go for
multiple lines> and also <a tag may
not be
finished...
Enter data:
<a tag>
</another tag>
<a tag may contain < character >
<a tag can go for multiple lines>
<a tag may not be finished.>

