C 如何计算单词列表中的单词实例

C 如何计算单词列表中的单词实例,c,search,sequential,C,Search,Sequential,我需要在C语言的单词列表中搜索一个单词,并计算该单词的出现次数 以下是我迄今为止所尝试的: { char *namePtr , *newnamePtr ; char newname[1][8] ; int occurrence ; namePtr=&name[i]; printf("enter the word you want to count : \n"); scanf("%s",newname[1][8]); for(i=0; i

我需要在C语言的单词列表中搜索一个单词,并计算该单词的出现次数

以下是我迄今为止所尝试的:

{
    char *namePtr , *newnamePtr ; char  newname[1][8] ;
    int occurrence ;

    namePtr=&name[i];
    printf("enter the word you want to count : \n");
    scanf("%s",newname[1][8]);
    for(i=0; i< N ; i++)
    {
        if(strcmp(namePtr,newnamePtr)==0)
            occurrence++ ;
        else
            if(strcmp(namePtr,newnamePtr)!=0)
                printf("this word does not exist in  the list!");
    }
    printf("the occurrence  of this word is %d",occurrence) ;
}
{
char*namePtr,*newnamePtr;char newname[1][8];
int发生;
namePtr=&name[i];
printf(“输入要计数的单词:\n”);
scanf(“%s”,newname[1][8]);
对于(i=0;i

如何计算单词列表中单词的出现次数?

要在由分隔符分隔的句子/单词列表中搜索单词,可以使用和循环查找该单词的所有出现次数

一个样本可能是这样的

//strstr returns a pointer to first occurrence of the string found else NULL

do
{
   p=strstr(str1,str2);
   if(p) count++;

}while(p);

还有一种方法可以实现同样的效果。

这里有一个简化版本:

char newname[80];
int occurrence = 0;

printf("enter the word you want to count : \n");
scanf("%s", newname);
for (i = 0; i < N ; ++i)
{
    if( strcmp(name[i], newname) == 0 ) // access list directly
        ++occurrence;
    else // no need to check the inverse of the condition
        printf("this word does not exist in the list!");
}

printf("the occurrence  of this word is %d", occurrence) ;


如果要计算子字符串,可以使用以下代码:

int main(无效)
{
字符文本[]=“foo bar foo bar foo”;
字符字[]=“foo”;
char*p;
int发生率=0;
for(p=text;(p=strstrstr(p,word))!=NULL;p+=sizeof(word)-1)
{
事件++;
}
printf(“出现%s=%d\n”,单词,出现次数);
}
#包括
#包括
#定义n10
字符*名称[N]={
“the”、“name”、“pointer”、“the”、“count”,
“单词”、“想要”、“去”、“数”、“存在”
};
int main(){
字符*namePtr,*newnamePtr;
char newname[8];
int i,发生率=0;
printf(“输入要计数的单词:\n”);
scanf(“%s”,新名称);
newnamePtr=newname;
对于(i=0;i
从正确的布局开始;缩进到处都是。程序在执行过程中终止,但不计算发生的次数。@user3115387您看到的错误是什么?顺便说一句:您在上面粘贴的代码片段是主要功能吗?
charnewname[1][8]-->
charnewname[8]-->
scanf(“%s”,新名称)
if(strcmp(namePtr,newnamePtr)!=0)
移动到循环外-->
if(出现==0)
    if( strcmp(name[i], newname) == 0 ) // access list directly
    if( strstr(name[i], newname) != NULL ) // access list directly   
#include <stdio.h>
#include <string.h>

#define N 10

char *name[N] = {
    "the", "name", "pointer", "the", "count",
    "word", "want", "to", "count", "exist"
};

int main(){
    char *namePtr , *newnamePtr ;
    char  newname[8];
    int i, occurrence = 0;

    printf("enter the word you want to count : \n");
    scanf("%s", newname);
    newnamePtr = newname;
    for(i=0; i< N ; i++){
        namePtr = name[i];
        if(strcmp(namePtr, newnamePtr)==0)//strcmp(name[i], newname)
            occurrence++;
    }
    if(occurrence==0)
        printf("this word does not exist in  the list!");
    else
        printf("the occurrence  of this word is %d", occurrence);

    return 0;
}