我这里有一个新手问题,我想和你一起学习。 我有一个结构数组,在该数组内我有一个字符串变量。 我想计算字符串变量中包含单词“string”的所有位置。 我的问题是我不知道 string.h 函数使用什么来在字符串中搜索单词并对其进行计数。
希望你能帮助我。
这是我想要的一个简单示例。
struct _man
{
char name[50];
int house_number;
};
void main()
{
int i=0,count=0;
struct _man man[20];
//assuming there is already information inside the array struct
for(i = 0; i<20;i++)
{
if (//function i want to know to search for the word "Jose" inside string man[i].name)
{
count++;
}
}
printf("There is %d people with the word Jose in their name\n",count);
}
最佳答案
strstr()
是您需要使用的:
例如:
char * name = "Jose Pedro Birto";
char * toBeMatched = "Pedro";
if (strstr(name, toBeMatched)) { // or (strstr(name, toBeMatched) != null)
printf("name contains Pedro");
} else {
printf("name does not contain Pedro");
}
关于c - 在 C 中搜索字符串内的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23600521/