I'm trying to compensate for the carriage returns given to a program both from a file read in, or a word entered by a user followed by a carriage return.
The search function doesn't appear to deal with a \n very well:
Calling search:
Search function:Code:printf("Now, what would you like to search for?\n"); fgets(buffer, sizeof(buffer), stdin); sscanf(buffer, "%s", &query_word); search(root, query_word);
How can I compensate for the new-line characters when searching?Code:void search(struct node* rootnode, char* word) { struct node* new=NULL; /* a new node */ struct node* here=NULL; /* where we are looking */ struct node* root=NULL; here=rootnode; /* start at the root */ while (1) /* loop until I tell you to break */ { if (strcmp(word\n, here->data) == 0) { /* the word is right here */ printf("The word %s is in the tree!\n",word); } else if (strcmp(word, here->data) < 0) /* need to look left */ { if (here->left == NULL) { /* we've reached the end, and can add the new node here */ break; } else /* move left and loop */ { here=here->left; if(strcmp(word, here->data) == 0) { printf("The word %s is in the tree!\n", word); } } } else if (strcmp(word, here->data) > 0) /* need to look right */ { if (here->right == NULL) { /* we've reached the end, and can add the new node here */ break; } else /* move right and loop */ { here=here->right; if(strcmp(word, here->data) == 0) { printf("The word %s is in the tree!\n", word); } } } else { printf("The word %s is not in the tree!\n", word); } } /*return; */ }



LinkBack URL
About LinkBacks




I used to be an adventurer like you... then I took an arrow to the knee.