-1

EDIT:

Question:

Write a program to find the number of times that a given word (i.e. a short string) occurs in a sentence (i.e. a long string!). Read data from standard input. The first line is a single word, which is followed by general text on the second line.

Sample Input:

the
the cat sat on the mat

Sample Output:

2

I tried using scanf before but it fails to read the entire sentence and just checks the first word and returns 1 as the answer instead of 2

The code is:

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

int main()
{
char s[200000], c[20], v = ' ';
int i = 0, j, f, n = 0;
gets(c);
gets(s);

while (i < strlen(s))
{
    j = 0;
    f = 0;
    while (j < strlen(c))
    {
        if (s[i++] != c[j++])
        {
            f = 1;
            i--;
            break;
        }
    }

    if ((f == 0) && (i == strlen(s) || s[i] == ' ') && (v == ' '))
    {
        n++;
        v = s[i++];
    }
    printf("%d\n", n);
    return 0;
}
}
A.Vik
  • 93
  • 7
  • Why don't you provide the definition of the problem too, instead of making us reverse engineer it? – szpanczyk Mar 01 '17 at 16:41
  • 2
    Please note that you execute `strlen()` in every iteration of your loops, yet they remain constant. You are wasting cycles. – Paul Ogilvie Mar 01 '17 at 16:42
  • By the way, please read the information on how to write a good question. I'm not saying it's very bad, but not exactly what this website is supposed to facilitate. – szpanczyk Mar 01 '17 at 16:44
  • @szpanczyk Added the exact question.@Paul Ogilvie Can you suggest an alternative? – A.Vik Mar 01 '17 at 16:44
  • Sorry.Didn't think it through – A.Vik Mar 01 '17 at 16:45
  • 3
    [Why is the gets function so dangerous that it should not be used?](http://stackoverflow.com/q/1694036/995714) – phuclv Mar 01 '17 at 16:46
  • The idea is that instead of: - asking for general help in your particular problem - you should ask for particular help in a general problem :) – szpanczyk Mar 01 '17 at 16:47
  • Ok.What is the alternative to using gets in this particular problem? – A.Vik Mar 01 '17 at 16:48
  • Can't you even click the link that You've been provided? :/ – szpanczyk Mar 01 '17 at 16:49
  • I saw that link.I tried using fgets and gets_s,they are giving errors?Is there any quick-fix to this? – A.Vik Mar 01 '17 at 16:53
  • @A.Vik: What errors are you getting with `fgets`? Also, realize you don't have to read the entire second line into memory all at once to do this - in fact, that's making your life harder at the moment. – John Bode Mar 01 '17 at 16:54
  • Too few arguments... – A.Vik Mar 01 '17 at 16:58
  • @A.Vik: Did you check the man page for `fgets` (or at least google for it)? It does take more arguments than `gets` (and has different behavior where newlines are concerned). – John Bode Mar 01 '17 at 17:02
  • 1
    I think that the problem of counting the occurrences of a _word_ in a sentence is only a particular case of the more general problem of counting the occurrences of a _substring_ in a string. I mean, given the word "the" and the sentence "the pen is there", what is your expected output, 1 or 2? – Bob__ Mar 01 '17 at 17:11
  • 1 should be the answer in that case – A.Vik Mar 01 '17 at 17:31
  • John Bode yes,I am right now trying to correct the code.... – A.Vik Mar 01 '17 at 17:32
  • "I tried using scanf before but it fails to read the entire sentence" --> lacks specificity. There are countless ways to use `scanf()`, some will work for you here, others will not. As this post lacks the `scanf()` code, the post is unclear as to what the problem was. – chux - Reinstate Monica Mar 01 '17 at 17:53

2 Answers2

0

This is the solution (answer) of the program you are trying to implement. This also provide the correct method of getting a string.

Hope this help in either ways!

Bidisha Pyne
  • 543
  • 2
  • 13
0

What is the alternative instead of using gets(c) and gets(s) in this code?

Instead of char c[20]; gets(c);, use the following

// Read a line of text
char c[20+1];  // 1 more for the \n
if (fgets(c, sizeof c, stdin) == NULL) {
  // Handle EOF or input error in some fashion
  puts("Unexpected EOF or input error");
  return 1;
}

Lop off the '\n' Removing trailing newline character from fgets() input

// lop off a potential trailing '\n'.
c[strcspn(c, "\n")] = '\0';

The rest of code may still have other issues.

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256