基于链表(指针替代字符数组)的文本纠错编程作业问题咨询
Hey there! Sounds like you've already nailed the tough setup stuff—stripping punctuation, reading files into pointer-based linked lists isn't trivial. Let's walk through the most common roadblocks people hit at the correction stage, plus actionable fixes to get your poem polished up.
Common Issues & Fixes
1. Reattaching Punctuation After Correction
Since you first stripped punctuation to match misspellings, the trick is to track the original punctuation for each word so you can reapply it post-correction.
First, update your poem linked list node to store both the clean (punctuation-free) word and the original:
typedef struct PoemWord { char* clean_word; // For matching misspellings char* original_word;// Original with punctuation intact struct PoemWord* next; } PoemWord;
Then, use a helper function to extract punctuation from the original word, and append it to the corrected term:
// Extract trailing punctuation from a word (e.g., "hello!" → "!") char* extract_punctuation(const char* word) { int len = strlen(word); int punct_start = len - 1; // Walk backwards until we hit a non-punctuation character while (punct_start >= 0 && ispunct(word[punct_start])) { punct_start--; } int punct_len = len - punct_start - 1; char* punct = malloc(punct_len + 1); strncpy(punct, word + punct_start + 1, punct_len); punct[punct_len] = '\0'; return punct; }
2. Matching Misspellings Across Linked Lists
When iterating through your poem list, cross-reference each clean word with your correction list (which should store misspelled → correct pairs). Always add null checks to avoid crashes:
typedef struct Correction { char* misspelled; char* correct; struct Correction* next; } Correction; void correct_poem(PoemWord* poem_head, Correction* corr_head) { PoemWord* poem_curr = poem_head; while (poem_curr != NULL) { Correction* corr_curr = corr_head; int found_match = 0; while (corr_curr != NULL && !found_match) { // Match clean word to misspelled entry if (strcmp(poem_curr->clean_word, corr_curr->misspelled) == 0) { // Get original punctuation char* punct = extract_punctuation(poem_curr->original_word); // Build corrected word with punctuation int new_len = strlen(corr_curr->correct) + strlen(punct) + 1; free(poem_curr->original_word); // Free old memory first! poem_curr->original_word = malloc(new_len); strcpy(poem_curr->original_word, corr_curr->correct); strcat(poem_curr->original_word, punct); free(punct); found_match = 1; } corr_curr = corr_curr->next; } poem_curr = poem_curr->next; } }
3. Handling Case Insensitivity
If your correction list uses lowercase but the poem has mixed case (e.g., "Teh" vs "teh"), add a helper to normalize case before matching:
// Convert string to lowercase for case-insensitive matching char* to_lower(const char* str) { char* lower = malloc(strlen(str) + 1); for (int i = 0; str[i] != '\0'; i++) { lower[i] = tolower((unsigned char)str[i]); } lower[strlen(str)] = '\0'; return lower; }
Update the match check to use lowercase versions:
char* poem_lower = to_lower(poem_curr->clean_word); char* misspelled_lower = to_lower(corr_curr->misspelled); if (strcmp(poem_lower, misspelled_lower) == 0) { // Correction logic... } free(poem_lower); free(misspelled_lower);
4. Avoiding Memory Leaks
Since you're using dynamic pointers, make sure to free all allocated memory once you're done:
- Free each node's
clean_wordandoriginal_wordbefore freeing the node itself - Free all correction list nodes and their string pointers
Final Output
Once corrections are done, iterate through the poem list and print each original_word to get the polished, punctuation-intact poem.
If you're hitting a specific snag—like weird punctuation edge cases, crashes during iteration, or mismatches I didn't cover—drop details and I can refine this further!
内容的提问来源于stack exchange,提问作者Tristan Shepherd

