简体   繁体   中英

Remove punctuation from string then put it back after replacement

My assignment was to read int input from two files. One contained a poem with misspelled words and the other contained a key with the misspelled word and the correct replacement right after.

I'm supposed to populate two linked lists with the information from each file and create a function that decodes the first file. I'm required to use pointers instead of char arrays in the linked list and at the end the program needs to print the first file with all corrections made.

I'm all good up until the decoder function needs to compare words with punctuation in them. How would i ignore punctuation without losing it in the final format.

Here's my decoder function:

LINK *decoder(TRANS *codet, LINK *head)
{
    LINK *currentt;
    currentt = head;
    TRANS *current;
    current = codet;
    printf("Decoding...\n");

    while (currentt != NULL)
    {
        current = codet;
        while (1)
        {
            if ()
            printf("Comparing %s with %s: \n", currentt->words, current->word1);
            if (!strcmp(currentt->words, current->word1))
            {
                printf("Replacing...\n");
                currentt->words = (char*)calloc(strlen(current->word2), sizeof(char));
                strcpy(currentt->words, current->word2);
                break;
            }
            current = current->next;
        }
        currentt = currentt->next;  
    }

    return head;
}

Here's the rest of my code:

//Tristan Shepherd

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

struct node
{
    char *words;
    struct node *next;
};

struct codex
{
    char *word1;
    char *word2;
    struct codex *next;
};

typedef struct node LINK;
typedef struct codex TRANS;

void printInsert(LINK *head) 
{
    printf("\n\nPrinting list: \n\n");
    LINK *current;
    current = head;
    while (current != NULL) {
        printf("%s ", current->words);
        current = current->next;
    } 
}

void printCodex(TRANS *codet) 
{
    printf("\n\nPrinting code: \n\n");
    TRANS *current;
    current = codet;
    while (current != NULL) {
        printf("%s %s\n", current->word1, current->word2);
        current = current->next;
    } 
}

void reverse(LINK **head)
{
    struct node *prev   = NULL;
    struct node *current = *head;
    struct node *next = NULL;
    while (current != NULL)
    {
        next  = current->next;  
        current->next = prev;   
        prev = current;
        current = next;
    }
    *head = prev;
}

LINK *insertList(char *wordt, LINK *head)
{
    LINK *current, *temp;
    temp = (LINK *)malloc(sizeof(LINK));
    temp->words = (char*)calloc(strlen(wordt)+1, sizeof(char));

    strcpy(temp->words, wordt);

    if (head == NULL)
    {
        head = (LINK *)malloc(sizeof(LINK));
        head = temp;
        temp->next = NULL;

        return head;
    }

    current = head;

    if (strcmp(current->words, wordt))
    {
        temp->next = current;
        head = temp;

        return head;
    }

    current = head;

    while (current != NULL)
    {
        if (current->next == NULL || strcmp(current->next->words, wordt))
        {
            temp->next = current->next;
            current->next = temp;

            return head;
        }
        current = current->next;
    }
}

TRANS *insertCodex(char *codeword, char *replace, TRANS *codet)
{
    TRANS *current, *temp;

    temp = (TRANS *)malloc(sizeof(TRANS));
    temp->word1 = (char*)calloc(strlen(codeword)+1, sizeof(char));
    temp->word2 = (char*)calloc(strlen(replace)+1, sizeof(char));

    strcpy(temp->word1, codeword);
    strcpy(temp->word2, replace);

    if (codet == NULL)
    {
        codet = (TRANS *)malloc(sizeof(TRANS));
        codet = temp;
        temp->next = NULL;

        return codet;
    }

    current = codet;

    if (strcmp(current->word1, codeword) && strcmp(current->word2, replace))
    {
        temp->next = current;
        codet = temp;

        return codet;
    }

    current = codet;

    while (current != NULL)
    {
        if (current->next == NULL || strcmp(current->next->word1, codeword) && strcmp(current->next->word2, replace))
        {
            temp->next = current->next;
            current->next = temp;

            return codet;
        }
        current = current->next;
    }
}

TRANS *scanCodex(FILE *code, TRANS *codet)
{
    char *codeword = (char*)malloc(13*sizeof(char));
    char *replace = (char*)malloc(13*sizeof(char));

    while(1)
    {
        fscanf(code, "%s %s", codeword, replace);
        if (feof(code)) break;
        codet = insertCodex(codeword, replace, codet);
    }
    fclose(code);

    return codet;
}

LINK *scanInsert(FILE *stream, LINK *head)
{
    char *input = (char*)malloc(13*sizeof(char));

    while (1)
    {
        fscanf(stream, "%s", input);
        if(feof(stream)) break;
        head = insertList(input, head);
    }

    fclose(stream);

    return head;
}

LINK *decoder(TRANS *codet, LINK *head)
{
    LINK *currentt;
    currentt = head;
    TRANS *current;
    current = codet;
    printf("Decoding...\n");

    while (currentt != NULL)
    {
        current = codet;
        while (1)
        {
            if ()
            printf("Comparing %s with %s: \n", currentt->words, current->word1);
            if (!strcmp(currentt->words, current->word1))
            {
                printf("Replacing...\n");
                currentt->words = (char*)calloc(strlen(current->word2), sizeof(char));
                strcpy(currentt->words, current->word2);
                break;
            }
            current = current->next;
        }
        currentt = currentt->next;  
    }

    return head;
}

int main (void)
{
    FILE *stream = fopen("hw10data.txt", "r");
    FILE *code = fopen("hw10codex.txt", "r");
    LINK *head;
    TRANS *codet;
    head = NULL;
    codet = NULL;

    head = scanInsert(stream, head);
    reverse(&head);
    printInsert(head);

    codet = scanCodex(code, codet);
    printCodex(codet);
    head = decoder(codet, head);
    printInsert(head);
    exit(0);

}

Here's my final code if you need it:

 //Tristan Shepherd #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> struct node { char *words; struct node *next; }; struct codex { char *word1; char *word2; struct codex *next; }; typedef struct node LINK; typedef struct codex TRANS; void delete(LINK **head, char *key) { LINK *temp = *head, *prev; if (temp != NULL && !strcmp(temp->words, key)) { *head = temp->next; free(temp); return; } while (temp != NULL && strcmp(temp->words, key)) { prev = temp; temp = temp->next; } if (temp == NULL) return; prev->next = temp->next; free(temp); } void printInsert(LINK *head, int aftersort) { printf("\\n\\nPrinting list: \\n\\n"); LINK *current; current = head; while (current != NULL) { if (aftersort) { printf("%s", current->words); } else { printf("%s ", current->words); } current = current->next; } } void printCodex(TRANS *codet) { printf("\\n\\nPrinting codex: \\n\\n"); TRANS *current; current = codet; while (current != NULL) { printf("%s %s\\n", current->word1, current->word2); current = current->next; } } void reverse(LINK **head) { struct node *prev = NULL; struct node *current = *head; struct node *next = NULL; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } *head = prev; } LINK *insertList(char *wordt, LINK *head) { LINK *current, *temp; temp = (LINK *)malloc(sizeof(LINK)); temp->words = (char*)calloc(strlen(wordt)+1, sizeof(char)); strcpy(temp->words, wordt); if (head == NULL) { head = (LINK *)malloc(sizeof(LINK)); head = temp; temp->next = NULL; return head; } current = head; if (strcmp(current->words, wordt)) { temp->next = current; head = temp; return head; } current = head; while (current != NULL) { if (current->next == NULL || strcmp(current->next->words, wordt)) { temp->next = current->next; current->next = temp; return head; } current = current->next; } } TRANS *insertCodex(char *codeword, char *replace, TRANS *codet) { TRANS *current, *temp; temp = (TRANS *)malloc(sizeof(TRANS)); temp->word1 = (char*)calloc(strlen(codeword)+1, sizeof(char)); temp->word2 = (char*)calloc(strlen(replace)+1, sizeof(char)); strcpy(temp->word1, codeword); strcpy(temp->word2, replace); if (codet == NULL) { codet = (TRANS *)malloc(sizeof(TRANS)); codet = temp; temp->next = NULL; return codet; } current = codet; if (strcmp(current->word1, codeword) && strcmp(current->word2, replace)) { temp->next = current; codet = temp; return codet; } current = codet; while (current != NULL) { if (current->next == NULL || strcmp(current->next->word1, codeword) && strcmp(current->next->word2, replace)) { temp->next = current->next; current->next = temp; return codet; } current = current->next; } } TRANS *scanCodex(FILE *code, TRANS *codet) { char *codeword = (char*)malloc(13*sizeof(char)); char *replace = (char*)malloc(13*sizeof(char)); while(1) { fscanf(code, "%s %s", codeword, replace); if (feof(code)) break; codet = insertCodex(codeword, replace, codet); } fclose(code); return codet; } LINK *scanInsert(FILE *stream, LINK *head) { char *input = (char*)malloc(13*sizeof(char)); while (1) { fscanf(stream, "%s", input); if(feof(stream)) break; head = insertList(input, head); } fclose(stream); return head; } LINK *decoder(TRANS *codet, LINK *head) { LINK *currentt; currentt = head; TRANS *current; current = codet; char *temp = (char*)malloc(33*sizeof(char)); while (currentt != NULL) { int CorP = 0; int punct = 0; int t = 0; current = codet; while (1) { if (!strcmp(currentt->words, current->word1)) { currentt->words = (char*)calloc(strlen(current->word2)+1, sizeof(char)); strcpy(currentt->words, current->word2); strcat(currentt->words, " "); if (punct == 1) { strtok(currentt->words, " "); strcat(currentt->words, ".\\n"); } if (punct == 2) { strtok(currentt->words, " "); strcat(currentt->words, ",\\n"); } if (!strcmp(currentt->words, "skip ")) { delete(&head, currentt->words); } break; } current = current->next; if (current == NULL) { strcpy(temp, currentt->words); if (!strcmp(currentt->words, strtok(temp, "."))) { if(!strcmp(currentt->words, strtok(temp, ","))) { if(t == 1) { strcat(currentt->words, " "); if (punct == 1) { strtok(currentt->words, " "); strcat(currentt->words, ".\\n"); } if (punct == 2) { strtok(currentt->words, " "); strcat(currentt->words, ",\\n"); } break; } t++; } else { strcpy(currentt->words, strtok(currentt->words, ",")); current = codet; punct = 2; } } else { strcpy(currentt->words, strtok(currentt->words, ".")); current = codet; punct = 1; } current = codet; } } currentt = currentt->next; } return head; } int main (void) { FILE *stream = fopen("hw10data.txt", "r"); FILE *code = fopen("hw10codex.txt", "r"); LINK *head; TRANS *codet; head = NULL; codet = NULL; head = scanInsert(stream, head); reverse(&head); printInsert(head, 0); codet = scanCodex(code, codet); printCodex(codet); head = decoder(codet, head); printInsert(head, 1); exit(0); } 

@David C. Rankin

Contents of the Files:

File 1:

Eye I eye I chequer checker Pea P Sea C plane plainly lee skip four for revue review Miss Mistakes Steaks skip knot not sea see quays keys whirred word weight wait Two To two to Weather Whether write right oar or aweigh away threw through Your You're shore sure two to no know Its It's vary very weigh way tolled told sew so bless blessed freeze frees yew you lodes loads thyme time right write stiles styles righting writing aides aids rime rhyme frays phrase come composed posed skip trussed trusted too to bee be joule jewel cheque check sum some

File 2:

Eye have a spelling chequer, It came with my Pea Sea. It plane lee marks four my revue, Miss Steaks I can knot sea. Eye strike the quays and type a whirred, And weight four it two say, Weather eye am write oar wrong, It tells me straight aweigh. Eye ran this poem threw it, Your shore real glad two no. Its vary polished in its weigh. My chequer tolled me sew. A chequer is a bless thing, It freeze yew lodes of thyme. It helps me right all stiles of righting, And aides me when eye rime. Each frays come posed up on my screen, Eye trussed too bee a joule. The chequer pours over every word, Two cheque sum spelling rule.

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