c - How to check if a particular string in one array is in another array -
i have array called puzzle consist of words/letters/random strings , want check if has of same strings in array called dictionary (the strings in dictionary listed in alphabetical order)
so believe problem binary search in program, i'm not entire sure how work around using strings. tried use strcmp() don't think thats way go?
when program runs, gets no output. there no matches there are.
here binary search function:
int binsearch(char **dictionary, char *puzzle) { int start = 1; //excluded first string of dictionary array bc # int end = listlength; while (start < end) { int mid = (start + end) / 2; int temp = strcmp(dictionary[mid], puzzle); if (temp < 0) { start = mid + 1; //it in upper half } else if (temp > 0) { //check lower half end = mid; } else return 1; //found match } return 0; }
and entire code here if maybe need see
#include <stdio.h> #include <stdlib.h> #include <string.h> #define listlength 149256 #define maxwordlen 19 char **getwords(int rows, int cols); void freearray(char **array, int rows); char **makegridarray(int rows, int cols); int binsearch(char **dictionary, char *puzzle); void wordsearch(char **dictionary, char **puzzle, int row, int col); const int dx_size = 8; const int dx[] = { -1, -1, -1, 0, 0, 1, 1, 1 }; const int dy[] = { -1, 0, 1, -1, 1, -1, 0, 1 }; int main() { //read in dictionary int i, j, x = 0, numcases, gridrow, gridcol; char **words = getwords(listlength, maxwordlen); //get number of cases. printf("enter number of cases:\n"); scanf("%d", &numcases); //process each case. while (x < numcases) { scanf("%d%d", &gridrow, &gridcol); //make word search grid char **grid = makegridarray(gridrow + 1, gridcol); /* testing if grid storing (i = 0; < gridrow + 1; i++) { printf("%s\n", grid[i]); } */ printf("words found grid #%d:\n", x + 1); wordsearch(words, grid, gridrow + 1, gridcol); x++; freearray(grid, gridrow + 1); } freearray(words, listlength); } char **getwords(int rows, int cols) { int i; //allocate top level of pointers. char **words = malloc(sizeof(char*) * rows); //allocate each individual array (i = 0; < rows; i++) { words[i] = malloc(sizeof(char) * cols + 1); } //read dictionary.txt file *dictionary = fopen("dictionary.txt", "r"); (i = 0; < rows; i++) { fgets(words[i], cols + 1,dictionary); } fclose(dictionary); return words; } char **makegridarray(int rows, int cols) { //allocate top level of pointers. char **grid = malloc(sizeof(char*) * rows); int i, j; //allocate each individual array (i = 0; < rows; i++) { grid[i] = malloc(sizeof(char) * cols + 1); } //read in user input grid (i = 0; < rows; i++) { gets(grid[i]); } return grid; } int binsearch(char **dictionary, char *puzzle) { int start = 1; //excluded first string of dictionary array bc # int end = listlength; while (start < end) { int mid = (start + end) / 2; int temp = strcmp(dictionary[mid], puzzle); if (temp < 0) { start = mid + 1; //it in upper half } else if (temp > 0) { //check lower half end = mid; } else return 1; //found match } return 0; } void wordsearch(char **dictionary, char **puzzle, int row, int col) { int i, x, y, dir; char wordsfound[19] = { '\0' }; (x = 0; x < row + 1; x++) { (y = 0; y < col; y++) { (dir = 0; dir < dx_size; dir++) //check every direction (i = 0; < 19; i++) { //will continue in direction dx,dy starting @ x,y int nextx = x + dx[dir] * i; int nexty = y + dy[dir] * i; if (nextx < 0 || nextx >= row) break; //keep in bounds if (nexty < 0 || nexty >= col) break; //store string of letters check wordsfound[i] = (puzzle[nextx][nexty]); if (i > 3) { //minimum word 4 wordsfound[i + 1] = '\0'; //if string of letters word, print int bin = binsearch(dictionary, wordsfound); if (bin) { printf("%s\n", wordsfound); } } } } } return; } void freearray(char **array, int rows) { //free arrays int i; (i = 0; < rows; i++) { free(array[i]); } free(array); }
the problem in getwords()
function: read words dictionary fgets()
forget remove trailing \n
. words in dictionary have trailing \n
, none of them match searches.
here corrected version:
char **getwords(int rows, int cols) { char line[256]; int i; //allocate top level of pointers. char **words = malloc(sizeof(char*) * rows); //read dictionary.txt file *dictionary = fopen("dictionary.txt", "r"); (i = 0; < rows; i++) { if (!fgets(line, sizeof line, dictionary)) line[0] = '\0'; line[strcspn(line, "\n")] = '\0'; words[i] = strdup(line); } fclose(dictionary); return words; }
note better not rely on known magical listlength. ignore comment , empty lines while reading dictionary.
Comments
Post a Comment