c - 在 C 中搜索字符串内的单词

标签 c string

我这里有一个新手问题,我想和你一起学习。 我有一个结构数组,在该数组内我有一个字符串变量。 我想计算字符串变量中包含单词“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/

相关文章:

c - 如何在C/C++中使用宏定义名称减少的变量?

c - 如何定义常量结构

c - 每个进程内存中的共享库是如何寻址的?

c - uint8_t 数组 - 内存中的数据

php - 如何在 PHP 中检查两个字符串的部分相似性

c++ - lambda 采用 `char` 参数

java - 在Java中,我有一个名为字符串 "A"(大小1)的字符串。为什么 string.substring(1) 没有给出异常

java - 在java中将日期从字符串转换为oracle.jbo.domain.Date

c - 我应该相信调用库初始化函数有利于性能吗?

将0连接到C中的字符串