简体   繁体   中英

Why does i never get past 1?

I am trying to create a program to solve a wordsearch from a 2D array using only pointers. In my primary function for doing the actual search for the words I have a while loop that is supposed to keep going as long as i doesn't exceed the length of the word and as long as the letters from the word correspond to the letters on the grid. After finding the word it should make the letters on the grid lowercase and print out that it found the word. Here is my entire program right now.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void printPuzzle(char** arr, int n);
void searchPuzzle(char** arr, int n, char** list, int listSize);
void searchWord(int j, int k, int gridsize, char** arr, char** list, int listPos);

int main(int argc, char **argv) {
    int bSize = 15;
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <puzzle file name>\n", argv[0]);
        return 2;
    }
    int i, j;
    FILE *fptr;
    char **block = (char**)malloc(bSize * sizeof(char*));
    char **words = (char**)malloc(50 * sizeof(char*));

    fptr = fopen(argv[1], "r");
    if (fptr == NULL) {
        printf("Cannot Open Puzzle File!\n");
        return 0;
    }

    for(i=0; i<bSize; i++){
        *(block+i) = (char*)malloc(bSize * sizeof(char));

        fscanf(fptr, "%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c\n", *(block+i), *(block+i)+1, *(block+i)+2, *(block+i)+3, *(block+i)+4, *(block+i)+5, *(block+i)+6, *(block+i)+7, *(block+i)+8, *(block+i)+9, *(block+i)+10, *(block+i)+11, *(block+i)+12, *(block+i)+13, *(block+i)+14 );
    }
    fclose(fptr);

    fptr = fopen("states.txt", "r");
    if (fptr == NULL) {
        printf("Cannot Open Words File!\n");
        return 0;
    }

    for(i=0; i<50; i++){
        *(words+i) = (char*)malloc(20 * sizeof(char));
        fgets(*(words+i), 20, fptr);        
    }

    for(i=0; i<49; i++){
        *(*(words+i) + strlen(*(words+i))-2) = '\0';    
    }

    printf("Printing list of words:\n");
    for(i=0; i<50; i++){
        printf("%s\n", *(words + i));       
    }
    printf("\n");

    printf("Printing puzzle before search:\n");
    printPuzzle(block, bSize);
    printf("\n");

    searchPuzzle(block, bSize, words, 50);
    printf("\n");

    printf("Printing puzzle after search:\n");
    printPuzzle(block, bSize);
    printf("\n");

    return 0;
}

void printPuzzle(char** arr, int n){
    for(int i = 0; i < n; i++){
        printf("\n");
        for(int j = 0; j < 15; j++){
            printf("%c ", *(*(arr+i)+j));
        }
    }

}

void searchPuzzle(char** arr, int n, char** list, int listSize){
    for(int i = 0; i < listSize; i++){
        for(int j = 0; j < 15; j++){
            for(int k = 0; k < 15; k++){
                    searchWord(j, k, 15, arr, list, i);
                }
            }

        }

}


void searchWord(int j, int k, int gridsize, char** arr, char** list, int listPos){
    int wordlength = strlen(*(list+listPos));
    if(j+wordlength <= gridsize){ //Horizontal
        int i = 0;
        while(i < wordlength && *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
            i++;
        }
        if(i == wordlength){
            while(i > 0 && *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
            *(*(arr+(j+i))+k) = tolower(*(*(arr+(j+i))+k));
            i--;
            }
            printf("Word found: ");
            for(i = 0; i < wordlength; i++)
                printf("%c", *(*(list+listPos)+i));
        }
    }

    if(k+wordlength <= gridsize){ //Vertical
        int i = 0;
        while(i < wordlength && *(*(arr+j)+(k+i)) == *(*(list+listPos)+i)){
            i++;
        }
        if(i == wordlength){
            while(i > 0 && *(*(arr+j)+(k+i)) == *(*(list+listPos)+i)){
            *(*(arr+(j+i))+k) = tolower(*(*(arr+(j+i))+k));
            i--;
            }
            printf("Word found: ");
            for(i = 0; i < wordlength; i++){
                printf("%c", *(*(list+listPos)+i));
            }
        }
    }

    if(j+wordlength <= gridsize && k+wordlength <= gridsize){ //Diagonal
        int i = 0;
        while(i < wordlength && *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
            i++;
        }
        if(i == wordlength){
            while(i > 0 && *(*(arr+(j+i))+(k+i)) == *(*(list+listPos)+i)){
            *(*(arr+(j+i))+(k+i)) = tolower(*(*(arr+(j+i))+(k+i)));
            i--;
            }
            printf("Word found: ");
            for(i = 0; i < wordlength; i++)
                printf("%c", *(*(list+listPos)+i));
        }
    }



}

This is the part I believe there is a problem with.

while(i < wordlength && *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
            i++;
        }

If I print out the decimal value of i after this while loop it should at some point approach the length of one of the words as I know some words are horizontal, however every time it loops through and prints the value of i it never exceeds 1 which means the while loop never runs more than once which is impossible with the wordsearch files I am using to test this. What is causing this to happen? It is also 0 or 1 for every other direction when I try, the example above was just the while loop from the horizontal search.

There may also be a problem with the function searchPuzzle and the amount it loops through. However, I think that may be just my imagination.

Additional Info: The grid is 15x15 and there are 50 words in the list.

I think that you might need to declare Wordsearch again after the && -

while(i < wordlength && wordlength *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
            i++;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM