Search code examples
cfilepositionstrstr

strstr() function get the position


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.


Solution

  • I presumed that the program is intended to check each of the word list in the first file, for occurrence in the single text line of the second file, and with a few tweaks, it works.

    I added some error checking and removed the trailing newline from the file inputs. I checked the result of strstr() before printing values based on NULL. I also added another #define to distinguish the size of the stack from the length of the test string, and, I check the stack does not overflow.

    UPDATE revises the code to check whole words - case insensitive.

    #include<stdio.h>
    #include<string.h>
    
    #define WORDMAXLENGTH 30
    #define MAXENTRY 200
    #define MAXLENGTH 200
    
    typedef struct{
        char stack[MAXENTRY][WORDMAXLENGTH];
        int top;
    } stack;
    
    int main(){
        FILE *inFile;
        FILE *inFile2;
        int i, w;
        char myArr[MAXLENGTH];
        char *sptr; 
        stack query;
        query.top = 0;
        inFile = fopen("query.txt","r");
        inFile2 = fopen("hello.txt","r");
        if (inFile == NULL || inFile2 == NULL) {
            printf("Cannot open both files\n");
            return 1;
        }
        while(fgets(query.stack[query.top], WORDMAXLENGTH, inFile) != NULL){
            i = strcspn(query.stack[query.top], "\r\n");
            query.stack[query.top][i] = 0;      // remove trailing newline etc
            if (++query.top >= MAXENTRY)        // check stack full
                break;
        }
    
        fgets(myArr,MAXLENGTH,inFile2);
        //myArr [ strcspn(myArr, "\r\n") ] = 0; // remove trailing newline etc
        w = 1;                                  // word count
        sptr = strtok(myArr, " \t\r\n");        // removes trailing stuff anyway
        while (sptr) {                          // each word in test string
            for(i=0; i<query.top; i++) {        // each word in library list
                if (stricmp(sptr, query.stack[i]) == 0)  // without case
                    printf("%-4d %s\n", w, query.stack[i]);
            }
            w++;
            sptr = strtok(NULL, " \t\r\n");
        }
    
        fclose(inFile);
        fclose(inFile2);
        return 0;
    }
    

    File query.txt:

    cat
    dog
    fox
    rabbit
    

    File hello.txt:

    A quick brown fox jumps over the lazy dog
    

    Program output:

    4    fox
    9    dog