我想比较两个字符串,但我要找的字是40个字符的数组。所以我想要的是比较单词,与数组的前40个字符。
我该怎么做?
所以:
words[1] = "apple"
words2[1] = a
words2[2] = p
words2[3] = p
words2[4] = l
words2[5] = e
代码
int j;
for(j = 0; j < i; j++)
{
printf("%s\n", words[j]);
if (strcmp (words[j], words2[]) == 0)
{
// found it
}
}
答案 0 :(得分:1)
strstr将获取两个以空字符结尾的字符串,并告诉您是否在另一个字符串中。此示例在word
array
#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;
}