C 如何在strstr找到指定字符串时增加变量
我希望每次strstr找到一个包含“house”的字符串时,我的变量houseTot都会增加1。我在代码中基本上做到了这一点:C 如何在strstr找到指定字符串时增加变量,c,strstr,C,Strstr,我希望每次strstr找到一个包含“house”的字符串时,我的变量houseTot都会增加1。我在代码中基本上做到了这一点: struct Owner1_def { int totProps; char type[40]; float monthlyPrice; float maintenance; int bedrooms; int bathrooms; }; typedef struct Owner1_def Owner1; int main
struct Owner1_def
{
int totProps;
char type[40];
float monthlyPrice;
float maintenance;
int bedrooms;
int bathrooms;
};
typedef struct Owner1_def Owner1;
int main(void)
{
char *ptr;
int i = 0;
ptr = strstr(database[i].hometype, "house");
for(i = 0; i <= 3; ++i)
{
if (ptr != NULL)
houseTot++;
}
return 0;
}
struct Owner1_def
{
int-totProps;
字符类型[40];
浮动月费;
浮子维护;
室内卧室;
室内浴室;
};
typedef结构所有者1_def所有者1;
内部主(空)
{
char*ptr;
int i=0;
ptr=strstrstr(数据库[i].hometype,“house”);
对于(i=0;i对strstrstr()
的调用应在循环内移动,以便在每次迭代时检查数据库[i]中存储的值
for(i = 0; i <= 3; ++i) {
ptr = strstr(database[i].hometype, "house");
if (ptr != NULL)
houseTot++;
}
for(i=0;i)您需要提供更多的代码。对strstrstr()的调用不应该在循环中吗?这是实际的代码吗?@DavidBowling修复了我的问题;strstrstr()这对我来说是全新的,我看到的所有示例都没有在任何类型的循环中使用它,所以我猜测,谢谢!strstrstr返回子字符串(如果可用)。因此您的代码应该可以正常工作。打印数据库以查看它是否包含house并提供更多代码