There are two text, text a is content and text b is list out the words line by line. The program is to get the position of words from text b in the content.
This is my program:
#include<stdio.h>
#include<string.h>
#define WORDMAXLENGTH 30
#define MAXLENGTH 200
int main(){
typedef struct{
char stack[MAXLENGTH][WORDMAXLENGTH];
int top;
}stack;
stack query;
query.top = 0;
int i = 0, j = 0,q = 0;
char myArr[MAXLENGTH];
char *PosStr = NULL;
FILE *inFile = fopen("query.txt","r");
FILE *inFile2 = fopen("hello.txt","r");
while(fgets(query.stack[query.top],WORDMAXLENGTH,inFile) != NULL){
query.top++;
}
fgets(myArr,MAXLENGTH,inFile2);
for(i = 0; i < query.top; i++){
PosStr = strstr(myArr,query.stack[i]);//get the position of s2 (Q1)
printf("%d\n", PosStr - myArr + 1);
}
fclose(inFile);
fclose(inFile2);
return 0;
}
Q1. Is this equation right? If it is wrong, how can I get the position? If it is right, why I can't get the position correctly? In addition, some of the result of PosStr is 0.