c - 从字符串中删除标点符号,然后在替换后将其放回去

标签 c replace linked-list strcmp punctuation

我的任务是从两个文件中读取 int 输入。其中一个包含一首带有拼写错误单词的诗,另一个包含一把带有拼写错误单词的 key 以及后面的正确替换单词。

我应该用每个文件中的信息填充两个链接列表,并创建一个解码第一个文件的函数。我需要在链接列表中使用指针而不是字符数组,最后程序需要打印第一个文件并进行所有更正。

我一切都很好,直到解码器功能需要将单词与其中的标点符号进行比较。我如何忽略标点符号而又不会在最终格式中丢失它。

这是我的解码器函数:

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;
}

这是我的其余代码:

//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);

}

最佳答案

@大卫·C·兰金

文件内容:

文件1:

眼睛我 眼睛我 检查员 检查员 豌 bean P 海C 平面清楚地 李斯基 四个为 评论评论 错过错误 牛排跳过 结不 海见 码头礁 呼呼的词 重量等待 两到 两个到 天气 是否 写对 桨或 权衡 扔过 你的你是 肯定上岸 两个到 不知道 它是它 差异很大 称重方式 被告知 缝这样 祝福有福 卡住释放 紫杉你 矿脉负荷 百里香时间 右写 风格风格 纠正写作 助手艾滋病 雾凇韵 磨损短语 来组成 摆姿势跳过 值得信赖的 太...以至于不能 蜜蜂 焦耳 gem 检查检查 总结一些

文件2:

眼睛有拼写检查器, 它是和我的豌 bean 海一起来的。 飞机李标记了我的滑稽剧四, 牛排小姐我可以结海。 目光注视着码头,打出一阵呼呼声, 重量四它两个说, 天气之眼写错了桨, 它告诉我直接称重。 眼睛跑过这首诗扔它, 你的岸真高兴两个不。 它的重量各不相同。 我的检查员告诉我缝纫。 检查员是一件有福的事, 它卡住了红 bean 杉的百里香矿脉。 它帮助我纠正所有的错误, 并在眼霜时帮助我。 每一次的争吵都会出现在我的屏幕上, 眼睛也被束缚了一小焦耳。 检查员仔细检查每一个字, 两个校验和拼写规则。

关于c - 从字符串中删除标点符号,然后在替换后将其放回去,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49866661/

相关文章:

c - 指针与引用与常规按值传递 C++

MYSQL 替换数值

c - 我该怎么做才能使我的程序在 do while 循环中保持在开关中输入的最小数字?

python - 用来自两个不同列表的值替换列表的 boolean 值

Javascript 正则表达式替换不起作用

java - 按字母顺序对名称链接列表进行排序

Java - ArrayList<> 和 LinkedList<> - 作为标识符的类

c - C链表中的内存泄漏

c - 动态分配的二维矩阵的段错误

html - libxml2 HTML block 解析