0

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;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • ...and your point is? Does this code compile? What does it do? What do you want it to do instead? – underscore_d Apr 15 '16 at 16:35
  • I few things before going deeper. 1.- I recommend to read about [safe scanf](http://stackoverflow.com/questions/1621394/how-to-prevent-scanf-causing-a-buffer-overflow-in-c) you can have a potential overflow over there. 2.- Compiling your code, it dumps "unknown" strcmp. See if you are using the correct headers. – Joel Apr 15 '16 at 16:35
  • You *are* using pointers, so is your use of `strcmp` illegal? If not, you might as well use `strstr`. – Weather Vane Apr 15 '16 at 16:41
  • 1
    1) `#include ` --> `#include ` 2) `scanf("%[ ^\n]s",sentence);` --> `scanf("%99[^\n]%*c",sentence);` 3) `fgets(word,20,stdin);` --> `fgets(word,20,stdin); word[strcspn(word,"\n")] = 0;` 4) `if(sentence[i]==' ') j=0;` --> `if(sentence[i]==' '){ j=0; ++i; }` – BLUEPIXY Apr 15 '16 at 16:54
  • Indent your code please. – Jabberwocky Apr 15 '16 at 16:58

2 Answers2

1
  • The name of the header is string.h not strings.h.
  • use scanf(" %s",word); instead of fgets(word,20,stdin); to enter word.See the space before %s to consume whitespaces.
  • Also you have to add i++ after j=0 in your code,that is why it is running infinitely.

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;
}
Rajeev Singh
  • 3,292
  • 2
  • 19
  • 30
0
  1. Change #include <strings.h> to #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); instead. Don't forget to handle the '\n'.

  3. Remove that '\n' in word.

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;
}
nalzok
  • 14,965
  • 21
  • 72
  • 139