Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C strcmp将字符串与数组的前x个字符进行比较_C_Arrays_String_Strcmp - Fatal编程技术网

C strcmp将字符串与数组的前x个字符进行比较

C strcmp将字符串与数组的前x个字符进行比较,c,arrays,string,strcmp,C,Arrays,String,Strcmp,我想比较两个字符串,但我要查找的单词是40个字符的数组。所以我要做的是比较单词和数组的前40个字符 我该怎么做 因此: 代码 intj; 对于(j=0;j

我想比较两个字符串,但我要查找的单词是40个字符的数组。所以我要做的是比较单词和数组的前40个字符

我该怎么做

因此:

代码

intj;
对于(j=0;j
strstr将获取两个以null结尾的字符串,并告诉您其中一个是否在另一个字符串中。此示例在
数组中查找
word

#include<stdio.h>
#include<stdlib.h>

int main() {
    char array[] = {"an array of several words"};
    char word[] = { " of "};
    char *pchar = NULL;

    pchar = strstr ( array, word);
    if ( pchar) {
        printf ( "found %s in %s\n", word, array);
    }

    return 0;
}
#包括
#包括
int main(){
字符数组[]={“几个字的数组”};
字符字[]={of“};
char*pchar=NULL;
pchar=strstrstr(数组,字);
如果(pchar){
printf(“在%s中找到%s\n”,字,数组);
}
返回0;
}

@user3121023
array
确实需要以null结尾,只是为了指出outline,但它与问题有何关系?在本例中,数组似乎不是以null结尾的,我理解的问题是字符串等于数组的开头,而您的答案是另一个字符串中包含的字符串:-(。此外,您在字符串周围使用了毫无意义且相当混乱的方括号。无法理解如何得到+1。。。
int j;
for(j = 0; j < i; j++)
{
    printf("%s\n", words[j]);

    if (strcmp (words[j], words2[]) == 0)
    {
    // found it
    }
}
#include<stdio.h>
#include<stdlib.h>

int main() {
    char array[] = {"an array of several words"};
    char word[] = { " of "};
    char *pchar = NULL;

    pchar = strstr ( array, word);
    if ( pchar) {
        printf ( "found %s in %s\n", word, array);
    }

    return 0;
}