wordsearch

This is a discussion on wordsearch within the C Programming forums, part of the General Programming Boards category; My spanish teacher made me do a word search and then I realized how much I hated them. I decided ...

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    wordsearch

    My spanish teacher made me do a word search and then I realized how much I hated them. I decided I would write my own(so there will probably be a lot of questions regarding coming up). I have some code that doesn't seach backwards yet and it only searches a one rowed file. Don't tell me how to do stuff like diagnols yet I think I can get them, but I use strstr to to find the word in the file. I was wondering how to take the pointer it returns and figure out how far into the array it is. here
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void){
            char input[BUFSIZ],input2[BUFSIZ],file[BUFSIZ];
            int row,x=0,y=0;
            char *retval;
            FILE *infile;
            printf("Enter the file with the words:  ");
            fflush(stdout);
            fgets(input,sizeof input,stdin);
            input[strlen(input)-1]='\0';
            infile=fopen(input,"r");
            if(!infile){
                    fprintf(stderr,"Couldn't open file %s\n",input);
                    exit(1);
            }
            printf("Enter the word to search for:  ");
            fflush(stdout);
            fgets(input2,sizeof input2,stdin);
            input2[strlen(input2)-1]='\0';
            fgets(file,sizeof file,infile);
            row=strlen(file);
            row--;
            printf("%i",row);
            retval=strstr(file,input2);
            if(retval==NULL)
                    printf("Not in this row!\n");
            else{
                    printf("In this row!!\n");
            }
            return 0;
    }
    don't worry about the row= part that is for later. Also if you want could you see if my fgets are safe. Thanx

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    I was wondering how to take the pointer it returns and figure out how far into the array it is.
    Take the pointer you receive from strstr and subtract the beginning of the array from it. The result is a signed quantity (ptrdiff_t) that represents the range between the two pointers.
    Code:
    ptrdiff_t dist = retval - file;
    >Also if you want could you see if my fgets are safe.
    No, they aren't. You need to test for a NULL return value as well as whether or not the last character is indeed a newline. Otherwise you would both be smashing valid data and ignoring a potential error where the entire line is not properly read.
    Code:
    if (fgets(input, sizeof input, stdin) == NULL) {
        /* Handle error or EOF */
    }
    if (input[strlen(input) - 1] == '\n')
        input[strlen(input) - 1] = '\0';
    else {
        /* Handle incomplete data */
    }
    Also, to avoid calling strlen twice it would be better to save the result in a variable.
    Code:
    size_t slen;
    ...
    slen = strlen(input);
    if (input[slen - 1] == '\n')
        input[slen - 1] = '\0';
    Or use another of the myriad ways to replace the trailing newline with a nul in a string.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    thanx man. I was thinking of making another pointer that points to the beginning of file I didn't even think that file itself would work and thanx for the fgets help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  2. wordsearch
    By egomaster69 in forum C Programming
    Replies: 4
    Last Post: 01-23-2005, 10:55 PM
  3. wordsearch solver, need help
    By lakai02 in forum C Programming
    Replies: 3
    Last Post: 01-14-2003, 02:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21