【问题标题】:Remove punctuation from string then put it back after replacement从字符串中删除标点符号,然后在替换后将其放回
【发布时间】:2018-04-16 21:40:32
【问题描述】:

我的任务是从两个文件中读取 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);

}

【问题讨论】:

  • 基本上,您想知道如何使fscanf(stream, "%s", input); 与标点符号分开解析单词,即标记它们?您确定输入的诗歌需要被标记为链表,而不是简单地即时转换吗?
  • strcmp 不会区分字母和标点符号,但您可以轻松编写自己的比较函数并调用它
  • 发布数据文件的内容。否则,您没有提供A Minimal, Complete, and Verifiable Example (MCVE)

标签: c replace linked-list strcmp punctuation


【解决方案1】:

@大卫·C·兰金

文件内容:

文件 1:

眼睛我 我的眼睛 检查器 豌豆 P 海C 平淡无奇 李跳过 四为 评论评论 失误小姐 牛排跳过 结不 看海 码头钥匙 呼呼的词 重量等待 二至 两个到 天气 是否 写对 桨或 称重 穿过 你的你 岸上肯定 两个到 不知道 它是 变化很大 称重方式 收费告诉 缝起来 祝福祝福 冻结释放 你 矿脉载荷 百里香时间 写对 阶梯样式 扶正写作 助手艾滋病 韵律 磨损短语 冷静下来 构成跳过 受信任的桁架 太...以至于不能 蜜蜂 焦耳宝石 检查检查 总结一些

文件 2:

眼睛有一个拼写检查器, 它和我的豌豆海一起出现。 它平面李标志着我的四个讽刺, Miss Steaks 我可以结海。 目光扫过码头,敲出一声呼呼, 和重量四它两个说, 天气的眼睛我写错了桨, 它直接告诉我称重。 眼跑这首诗扔了它, 你的岸真的很高兴两个没有。 它的重量各不相同。 我的检查员告诉我缝纫。 检查器是一件有福的事情, 它冻结了百里香的紫杉矿脉。 它帮助我纠正所有扶正的阶梯, 并在眼霜时帮助我。 每一个冲突都出现在我的屏幕上, 眼睛也被捆绑了一个焦耳。 检查器倾注在每一个字上, 两个校验和拼写规则。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      相关资源
      最近更新 更多