一起talk C栗子吧(第六十九回:C语言实例--DIY字符串查找函数)

来源:互联网 发布:qq授权软件取消 编辑:程序博客网 时间:2024/04/29 12:31

各位看官们,大家好,上一回中咱们说的是DIY字符串长度函数的例子,这一回咱们说的例子是:DIY字符串查找函数。闲话休提,言归正转。让我们一起talk C栗子吧!

我们在前面的章回中介绍过字符串查找函数,时间不长,但是有些看官已经忘记了,为了加深看官们对字符串查找函数的印象,我们准备DIY字符串查找函数。Just do it by yourself!

我们在前面的章回中一共介绍了三个字符串查找函数:strchr,strrchr和strstr。接下来我们分别介绍如何DIY这三个字符串查找函数。

DIY strchr函数

char diy_strchr(const char s1, int ch)

  • 1.在s1所指向的字符串中,取出一个字符和ch进行比较;
  • 2.判断这两个字符的ASCII码值大小,如果它们的值不相等,那么返回第一步,比较下一个字符;否则, 进入下一步;
  • 3.如果它们的值相等,说明找到了待查找的字符。这时停止查找;
  • 4.如果在查找过程中遇到了s1的小尾巴,那么说明没有找到ch。返回空指针;

下面是我写的代码,请大家参考:

char *diy_strchr(const char * s1, int ch){    char t;    const char *pStr = NULL;    if(NULL == s1)        return NULL;    t = ch;    pStr = s1;    while(*pStr != '\0')    {        if(*pStr == t)            break;        else            pStr++;    }    if(*pStr == t) // if ch is found, return the end of s1        return pStr;    else          // can't find ch in s1        return NULL;}

从上面的函数中,我们可以看到,字符串的小尾巴也是参与查找过程的,代码中在while循环后面的if语句就是专门来确认待查找的字符是不是字符串末尾的小尾巴。

下面是标准库中strchr的代码,请大家和我们DIY的代码进行比较:

/** * strchr - Find the first occurrence of a character in a string * @s: The string to be searched * @c: The character to search for */char *strchr(const char *s, int c){        for (; *s != (char)c; ++s)                if (*s == '\0')                        return NULL;        return (char *)s;}

通过对比,大家可以发现,标准库的代码比我们DIY的代码简洁一些,不过查找的思路是相同的,或者说的专业一点:查找的算法是相同的。标准库在返回s的时候,强制进行了类型转换,我们没有,因此在编译的时候会有警告:把const char类型的指针赋值给char类型的指针。

DIY strrchr函数

char diy_strrchr(const char s1, int ch)

  • 1.在s1所指向的字符串中,从其尾部(包含字符串末尾的空字符)取出一个字符和ch进行比较;
  • 2.判断这两个字符的ASCII码值大小,如果它们的值不相等,那么返回第一步,比较前一个字符;否则, 进入下一步;
  • 3.如果它们的值相等,说明找到了待查找的字符。这时停止查找;
  • 4.如果在查找过程中已经到达s1的头部,那么说明没有找到ch。返回空指针;

下面是我写的代码,请大家参考:

char *diy_strrchr(const char * s1, int ch){    char t;    const char *pStr = NULL;    if(NULL == s1)        return NULL;    t = ch;    pStr = s1;    while(*pStr != '\0')        pStr++;    while(pStr != s1)    {        if(*pStr == t)            break;        else            pStr--;    }    if(*pStr == t)        return pStr;    else          // can't find ch in s1        return NULL;}

通过上面的代码,大家可以发现,DIY函数strrchr和strchr的原理类似,只不过,一个是从字符串的头部开始向尾部方向进行查找,另外一个是从字符串的尾部开始向头部方向进行查找。

下面是标准库中strrchr的代码,请大家和我们DIY的代码进行比较:

/** * strrchr - Find the last occurrence of a character in a string * @s: The string to be searched * @c: The character to search for */char *strrchr(const char *s, int c){        const char *last = NULL;        do {                if (*s == (char)c)                        last = s;        } while (*s++);        return (char *)last;}

通过对比上面的代码,大家可以发现,标准库中的代码比我们DIY的代码少了一个循环,而且标准库使用查找算法和我们不同,它和strchr的算法类似,只是在查找到ch后没有停止查找过程,而是继续查找,直到遇到字符串的小尾巴为止。

DIY strstr函数

char diy_strstr(const char s1, const char *s2)

  • 1.在s1和s2所指向的字符串中,分别取出一个字符来进行比较;
  • 2.判断这两个字符的ASCII码值大小,如果它们的值不相等,那么返回第一步,比较s1中的下一个字符; 否则,进入下一步;
  • 3.如果它们的值相等,分别比较s1和s2所指字符串的下一个字符;
  • 4.反复进行步骤3,如果该过程中s1和s2中有不相同的字符,那么回到步骤1,重新从s1的下一个字符开始查找。
  • 5.如果在上一步中已经到达s2的尾部,那么说明在s1中找到了s2。返回s2在s1中的位置,如果已经到达 s1的尾部,那么说明在s1中没有找到s2,这时返回空指针;

下面是我写的代码,请大家参考:

char *diy_strstr(const char * s1, const char *s2){    const char *pStr1 = NULL;    const char *pStr2 = NULL;    int flag = 0;    if(NULL == s1 || NULL == s2)        return NULL;    while(*s1 != '\0' && *s2 != '\0')    {        if(*s1 == *s2)        {            pStr1 = s1;            pStr2 = s2;            while(*pStr1 != '\0' && *pStr2 != '\0')            {                if(*pStr1 == *pStr2) // if the char is equal ,compare next char                {                    pStr1++;                    pStr2++;                }                else                {                    flag = 1;                    break;                }            }            if(flag) // if substring is not equal ,compare next char of main string            {                flag = 0;                s1++;            }            else            {                if(*pStr2 == '\0') // if it is equal, return location of it                    return s1;                else                    break;            }        }        else //if the first char is not equal ,compare next char of main string        {            s1++;        }    }    return NULL;}

大家从上面的代码可以看到,整个查找过程是一个字符接着一个字符进行比较,因此比较耗费时间,其实,字符串查找函数有专门的算法,常见的是KMP算法,大家可以查阅算法相关的书箱中关于KMP算法的讲解。

下面是标准库中strstr的代码,请大家和我们DIY的代码进行比较:

/** * strstr - Find the first substring in a %NUL terminated string * @s1: The string to be searched * @s2: The string to search for */char *strstr(const char *s1, const char *s2){        size_t l1, l2;        l2 = strlen(s2);        if (!l2)                return (char *)s1;        l1 = strlen(s1);        while (l1 >= l2) {                l1--;                if (!memcmp(s1, s2, l2))                        return (char *)s1;                s1++;        }        return NULL;}

通过对比,大家可以发现,标准库中使用的算法和我们DIY时使用的算法完全不同,标准库使用了memcmp函数,但是,在本质上也是一个字符接着一个字符进行比较。另外,大家可以看到空字符是不参与查找的,当查找空字符时,直接返回s1指向的字符串。

看官们,我把这三个DIY函数整理成了一个文件,并且添加了详细的注释,除此之外,我还使用了前面章回中的测试case进行测试。正文中就不写代码了,详细的代码放到了我的资源中,大家可以点击这里下载。前面章回中的程序可以点击这里进行下载。

下面是程序的运行结果,大家可以和前面章回中的结果进行比较:

a(97) is found in abcdABCDabcd, and location is 1. a(97) is found in abcdABCDabcd, and re-location is 9. (0) is found in abcdABCDabcd, and location is 13. (0) is found in abcdABCDabcd, and re-location is 13. ABC is found in abcdABCDabcd, and location is 5.  is not found in abcdABCDabcd.

各位看官,关于DIY字符串查找函数的例子咱们就说到这里。欲知后面还有什么例子,且听下回分解。


0 0