word frequency in a text

This program counts how many times does a word appear in a text.

Can someone please explain why i have to add 1 to p in the while loop?

Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstring>

using namespace std;
char s[200], c1[20], *p;
int main()
{
    int k=0;
    cin.get(s,200);
    cin.get();
    cin>>c1;
    p=strstr(s,c1);
    while(p){
        k++;
        p=strstr(p+1,c1);
    }
    cout<<k;
    return 0;
}
Can someone please explain why i have to add 1 to p in the while loop?


Read: http://www.cplusplus.com/reference/cstring/strstr/
Application of logic should finish the job.
Topic archived. No new replies allowed.