I have written this code to find number of occurrences of a word in a line.
It worked fine on my Cygwin terminal.
But now when I run it on my Linux machine it is crashing at strcpy(line, strstr(line,word))
Why such behaviour?
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int findWord(char* line,char* word)
{
int count=0;
int wordLen = strlen(word);
while(true)
{
if( strstr(line,word) == NULL)
break;
strcpy(line, strstr(line,word)); //Crashes here
printf("@@%s\n",line);
strcpy(line,line+wordLen+1);
printf("##%s\n",line);
count++;
}
printf("%d\n",count);
return count;
}
int main()
{
cout << findWord("One Two Three Four One Two One Four","Three") << endl;
system("PAUSE");
return 0;
}
EDIT: Edited the title . This is common to both C and C++