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



我的任务是从两个文件中读取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("nnPrinting list: nn");
LINK *current;
current = head;
while (current != NULL) {
printf("%s ", current->words);
current = current->next;
} 
}
void printCodex(TRANS *codet) 
{
printf("nnPrinting code: nn");
TRANS *current;
current = codet;
while (current != NULL) {
printf("%s %sn", 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);
}

@David C. Rankin

文件内容:

文件 1:

眼睛 I 眼睛 I 棋盘检查器 豌豆· 海 C 平面明了 李跳 四个为 评论评论 错过错误 牛排跳过 不结 海见 码头钥匙 嗡�� 体重等待 二到 二到 天气是否 写对了 桨或 阿克扬 扔了过去 你的 岸上确定 二到 不知道 它的 变化很大 称重方式 托尔德告诉 缝纫所以 祝福祝福 冻结释放 紫杉你 矿脉负载 百里香时间 右写 斯泰尔斯风格 纠正写作 助手艾滋病 里姆押韵 磨损短语 来组成 姿势跳过 桁架可信 太到 蜜蜂是 焦耳宝石 支票支票 总结一些

文件 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("nnPrinting list: nn");
	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("nnPrinting codex: nn");
	TRANS *current;
current = codet;
while (current != NULL) 
{
printf("%s %sn", 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);
}

相关内容

  • 没有找到相关文章

最新更新