简体   繁体   English

C中字符串中出现的次数

[英]number of occurrences in a string in C

Given a array string, I have to enter a word and find the occurrences of the word in the string, however I cannot enter the word for which I need to find the occurrence. 给定一个数组字符串,我必须输入一个单词并在字符串中找到该单词的出现,但是我无法输入需要为其查找出现的单词。 I cannot use pointers as it hasn't been covered in the syllabus. 我不能使用指针,因为它在课程提纲中没有涉及。

#include <stdio.h>
#include <strings.h>
int main()
{
    char sentence[100],word[20],temp[20];
    int i=0,j=0,occurrences=0;
    scanf("%[ ^\n]s",sentence);
    printf("Enter the word to be searched:\n");
    fgets(word,20,stdin);
    while(sentence[i]!='\0')
    {
        while(sentence[i]!=' '&&sentence[i]!='\0')
        {
            temp[j++]=sentence[i];
            i++;
        }
        temp[j]='\0';

        if((strcmp(temp,word))==0)
        occurrences++;

        if(sentence[i]==' ')
        j=0;
    }
    printf("Number of Occurrences of the word are %d",occurrences);
    return 0;
}
  • The name of the header is string.h not strings.h . 标头的名称是string.h而不是strings.h
  • use scanf(" %s",word); 使用scanf(" %s",word); instead of fgets(word,20,stdin); 而不是fgets(word,20,stdin); to enter word .See the space before %s to consume whitespaces. 输入word %s查看%s之前%s空格以使用空格。
  • Also you have to add i++ after j=0 in your code,that is why it is running infinitely. 另外,您还必须在代码中j=0之后添加i++ ,这就是为什么它无限运行的原因。

You code should be like this: 您的代码应如下所示:

#include <stdio.h>
#include <string.h> //fix 1
int main()
{
    char sentence[100],word[20],temp[20];
    int i=0,j=0,occurrences=0;
    scanf("%[^\n]s",sentence);
    printf("Enter the word to be searched:\n");
    scanf(" %s",word); //fix 2
    while(sentence[i]!='\0')
    {
        while(sentence[i]!=' '&&sentence[i]!='\0')
        {
            temp[j++]=sentence[i];
            i++;
        }
        temp[j]='\0';

        if((strcmp(temp,word))==0)
        occurrences++;
        if(sentence[i]==' ')
        {
            j=0;i++; //fix 3
        }
    }
    printf("Number of Occurrences of the word are %d",occurrences);
    return 0;
}
  1. Change #include <strings.h> to #include <string.h> . #include <strings.h>更改为#include <string.h>

  2. scanf("%[ ^\\n]s", sentence); won't function as you expect. 将无法按预期运行。 To enter strings containing whitespace characters, you should use fgets(word, 20, stdin); 要输入包含空格字符的字符串,应使用fgets(word, 20, stdin); instead. 代替。 Don't forget to handle the '\\n'. 不要忘记处理'\\ n'。

  3. Remove that '\\n' in word . 删除word “ \\ n”。

BTW, the following code seems clearer to me: 顺便说一句,下面的代码对我来说似乎更清晰:

#include <stdio.h>
#include <string.h>

int main()
{
    char sentence[100], word[20];
    int occurrences = 0, i = 0;
    fgets(sentence, 100, stdin);
    sentence[strcspn(sentence, "\n")] = '\0';
    printf("Enter the word to be searched:\n");
    fgets(word, 20, stdin);
    word[strcspn(word, "\n")] = '\0';
    while(strlen(word) + i <= strlen(sentence))
    {
        if(memcmp(word, sentence + i, strlen(word) - 1) == 0)
            occurrences++;
        i++;
    }
    printf("Number of Occurrences of the word are %d", occurrences);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM