uva - 10815 - Andy's First Dictionary

本文介绍了一个基于字典的单词搜索与排序算法,包括单词的查找、插入和排序过程。通过将输入字符串转换为小写并逐个字符进行处理,实现对单词的搜索和插入操作,并最终对字典进行排序。
#define Local
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
char Dict[50000][250];

void SearchInsertDict (char word[], int *len_dict)
{
	int i = 0, j = 0, flag = 1;
	for (i = 0; i < *len_dict; i++)
	{
		if (0 == strcmp(word, Dict[i]))//==0字典里存在这个单词
		{
			flag = 0;
			break;
		}
	}
	if (flag)//不存在
	{
		strcpy(Dict[*len_dict], word);
		(*len_dict)++;
	}
}

void Strlwr(char s[])
{
	for (int i = 0; i < strlen(s); i++)
		if (s[i] >= 'A' && s[i] <= 'Z')
			s[i] = s[i] + 32;
}

int cmp(const void *a, const void *b)
{
	return strcmp((char *)a, (char *)b);
}

int main()
{
#ifdef Local
	freopen("a.in", "r", stdin);
	//freopen("a.out", "w", stdout);
#endif
	int i = 0, j = 0, k = 0, len_dict = 0;
	char sen[250], word[250];
	while (gets(sen) != NULL)
	{
		Strlwr(sen);
		for (i = 0; i < strlen(sen); i++)
		{
			int flag = 0;
			memset(word, '\0', sizeof(word));
			for (j = 0; sen[i] >= 'a' && sen[i] <= 'z'; i++, j++)
			{
				word[j] = sen[i];
				flag = 1;
			}
			if (flag)
			{
				word[j] = '\0';
				SearchInsertDict(word, &len_dict);
			}
		}
	}
	qsort(Dict, len_dict, sizeof(Dict[0]), cmp);
	for (i = 0; i < len_dict; i++)
		puts(Dict[i]);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值