📜  C/C++ 中的 HTML 解析器

📅  最后修改于: 2021-10-29 04:03:13             🧑  作者: Mango

HTML 解析器是一个程序/软件,通过它可以提取有用的语句,留下 html 标签(如

等)。

例子:

方法:让输入的字符串为大小为N的S,按照以下步骤解决问题:

  • 声明两个变量,开始结束 指向语句的起点和终点。
  • 遍历字符串,S 使用变量 i 如果 S[i] 等于 ‘>’,则将起始变量更新为 i+1 并跳出循环。
  • 通过在 S[start] 等于 ‘ ‘ 时运行一个循环来删除开头的空格,并在每次迭代中将start变量增加 1。
  • 同样,使用变量 i 从头开始遍历字符串S,如果 S[i] 等于 ‘<‘,则将结尾更新为 i-1 并跳出循环。
  • 运行一个循环并打印 [start, end] 范围内的字符串S 的字符。

HTML解析器

下面是上述方法在C 语言中的实现

// C program for the above approach
  
#include 
#include 
#include 
  
// Function to parse the HTML code
void parser(char* S)
{
    // Store the length of the
    // input string
    int n = strlen(S);
    int start = 0, end = 0;
    int i, j;
  
    // Traverse the string
    for (i = 0; i < n; i++) {
        // If S[i] is '>', update
        // start to i+1 and break
        if (S[i] == '>') {
            start = i + 1;
            break;
        }
    }
  
    // Remove the blank spaces
    while (S[start] == ' ') {
        start++;
    }
  
    // Traverse the string
    for (i = start; i < n; i++) {
        // If S[i] is '<', update
        // end to i-1 and break
        if (S[i] == '<') {
            end = i - 1;
            break;
        }
    }
  
    // Print the characters in the
    // range [start, end]
    for (j = start; j <= end; j++) {
        printf("%c", S[j]);
    }
  
    printf("\n");
}
  
// Driver Code
int main()
{
    // Given Input
    char input1[] = "

This is a statement

";     char input2[] = "

         This is a statement with some spaces

";     char input3[] = "

This is a statement with some @ #$ ., / special characters

         ";        printf("Parsed Statements:\n");        // Function Call     parser(input1);     parser(input2);     parser(input3);        return 0; }
输出:
Parsed Statements:
This is a statement
This is a statement with some spaces
This is a statement with some @ #$ ., / special characters

下面是上述方法在C++ 语言中的实现

// C++ program for the
// above approach
#include 
using namespace std;
  
// Function to parse the
// HTML code
void parser(char* S)
{
    // Store the length of the
    // input string
    int n = strlen(S);
    int start = 0, end = 0;
  
    // Traverse the string
    for (int i = 0; i < n; i++) {
        // If S[i] is '>', update
        // start to i+1 and break
        if (S[i] == '>') {
            start = i + 1;
            break;
        }
    }
  
    // Remove the blank space
    while (S[start] == ' ') {
        start++;
    }
  
    // Traverse the string
    for (int i = start; i < n; i++) {
        // If S[i] is '<', update
        // end to i-1 and break
        if (S[i] == '<') {
            end = i - 1;
            break;
        }
    }
  
    // Print the characters in the
    // range [start, end]
    for (int j = start; j <= end; j++) {
        cout << S[j];
    }
  
    cout << endl;
}
  
// Driver Code
int main()
{
    // Given Input
    char input1[] = "

This is a statement

";     char input2[] = "

         This is a statement with  some spaces

";     char input3[] = "

This is a statement with some @ #$ ., / special characters

         ";        cout << "Parsed Statements:\n";        // Function Call     parser(input1);     parser(input2);     parser(input3);     return 0; }
输出:
Parsed Statements:
This is a statement
This is a statement with  some spaces
This is a statement with some @ #$ ., / special characters

时间复杂度 在)
辅助空间:O(1)

注意:这个程序一次只解析一个语句。

想要从精选的视频和练习题中学习,请查看C 基础到高级C 基础课程