从字符串中删除标点符号,然后在更换后将其放回原处

时间:2018-04-16 21:40:32

标签: c replace linked-list strcmp punctuation

我的任务是从两个文件中读取int输入。一个包含一首拼写错误的单词,另一首包含一个带有拼错单词的键和正确的替换词。

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

我很好,直到解码器功能需要比较带有标点符号的单词。如何在不丢失最终格式的情况下忽略标点符号。

这是我的解码器功能:

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

}

2 个答案:

答案 0 :(得分:0)

如果您需要,这是我的最终代码:

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

}

答案 1 :(得分:0)

@David C. Rankin

文件内容:

文件1:

眼睛我 眼睛我 检查员检查 豌豆P. 海C 平坦地说 李跳过 四个 讽刺审查 错过错误 牛排跳过 不结 海见 码头钥匙 whirred字 重量等 两个到 两到 天气是否 写得对 桨或 更重要的是 扔了 你是你 岸边肯定 两到 不知道 它是的 变化很大 称重的方式 收费告诉 缝了 祝福祝福 冻结 你好吗 lodes负载 百里香时间 正确的写 两种风格 正确的写作 助手 雾韵 frays短语 来了 跳过了 受托信任 太...以至于不能 蜜蜂 焦耳宝石 检查一下 总结一下

文件2:

眼睛有拼写检查, 它来自我的豌豆海。 它的飞机里面标志着我的四个讽刺, Steaks小姐,我可以打结海。 眼睛瞄准码头并打出一声嗡嗡声, 重量四,两个说, 天气之眼我写错了, 它告诉我直接的重量。 眼睛把这首诗扔了, 你的岸真的很高兴两个没有。 它的重量各不相同。 我的检查员叫我缝了。 检查是一件幸事, 它冻结了百里香的红豆杉。 它帮助我纠正了扶正的所有障碍, 在眼睛雾霾的时候帮助我。 每个小故障都出现在我的屏幕上, 眼睛盯着太蜜蜂了。 检查员倾注每一个字, 两个校验和拼写规则。