首页 > 解决方案 > 确定从某个单元格开始的数组是否包含字符串的函数?

问题描述

*** C 新手在这里。***

我的程序有一个包含 HTML 文档的数组(每个 HTML 字符一个数组单元格)。我有一个循环遍历数组的每个单元格。在每个数组单元格中,我检查单元格值是否是某个字符串的开头;例如,当前单元格是否包含“&”,下一个单元格是否包含“n”和下一个“b”,然后是“s”,然后是“p”,然后是“;” (即," ")。

我写了一个函数来做比较。见下文。C 是否已经有一个可以进行这种比较的函数?如果不是,我在下面展示的函数是 C 专家如何编写的吗?如果不是,C 专家将如何编写该函数?

// p points to a cell in an array
// s is a string
// This function answers the question: does p start with s?
int cmpString(char *p, char *s) {
    char *q = p;  // Don't modify the position of the array pointer so make a copy
    
    while (*s != 0) {
       if (tolower(*q) != tolower(*s))
          return 0; 
       q++;
       s++;
    }
    return 1; 
}

标签: c

解决方案


推荐阅读