c++ - 关于c语言 strstr函数的问题
伊谢尔伦
伊谢尔伦 2017-04-17 13:43:38
0
2
830

先上代码吧

#include <stdio.h>
#include <string.h>
int main ()
{
    char test[]="jei123";
    char key[]="jeid23";
    int length = strlen(key);
    for(int i=0;i<length;i++)
    {
        if(strstr(test,&key[i])!=NULL)
        {
            printf ("%c已找到\n",key[i]);
        }
        else
        {
            printf ("%c失踪\n",key[i]);
        }
    }
    return 0;
}

运行结果是

讲道理j e i都应该被找到的

接下来换代码

#include <stdio.h>
#include <string.h>
int main ()
{
    char test[]="jei123";
    char key[]="jei123"; //更换此处代码
    int length = strlen(key);
    for(int i=0;i<length;i++)
    {
        if(strstr(test,&key[i])!=NULL)
        {
            printf ("%c已找到\n",key[i]);
        }
        else
        {
            printf ("%c失踪\n",key[i]);
        }
    }
    return 0;
}

此时又能被找到,求解

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
Peter_Zhu

strstr (str1, str2) function is used to determine whether the string str2 is a substring of str1. If so, return the first address of str2 appearing in str1
str1, str2 is two char type pointers

char test[]="jei123";       
char key[]="jeid23";
for(int i=0;i<length;i++)
{
    if(strstr(test,&key[i])!=NULL) 
    判断 " jeid23"  是  "  jei123"  的子串  明显不是,所以返回为空  
    判断 " eid23"   是  "  jei123"  的子串  明显不是,所以返回为空 
    以此类推
    只会找到  2  3
   
  

Change strstrt() to strchr() to get the result you want

小葫芦

Remove your terrible address character. Take a look & let’s

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template