读取带标点的英文段落,分词、去重按字典顺序输出(C语言)

例如:
输入:
I love Beijing.
I Love China.
Oh,Beijing is the capital of China.
Do you know?
Cheer up!!
输出:
Beijing
Cheer
China
Do
I
Love
Oh
capital
is
know
love
of
the
up
you

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

void sort(char str[][100], int n)
{
	int i, j;
	char s[100] = {'\0'};
	for(i = 0; i < n; i++)
	{
		for(j = 0; j < n - i -1; j++)
		{
			if(strcmp(str[j], str[j+1]) > 0)
			{
				strcpy(s, str[j]);
				strcpy(str[j], str[j+1]);
				strcpy(str[j+1], s);
			}
			else if(strcmp(str[j], str[j+1]) == 0)
			{
				str[j][0] = '\0';
			}
		}
	}
}

int main()
{
	char str[1000][100] = {"\0"};
	int i, j, count;
	freopen("data.txt", "r", stdin);
	count = 0;
	while(~scanf("%s", str[count++]));
	for(i = 0; i < count; i++)
	{
		j = 0;
		while(isalpha(str[i][j]))
		{
			j++;
		}
		str[i][j] = '\0';
	}
	sort(str, count);
	for(i = 0; i < count; i++)
	{
		if(strlen(str[i]) != 0)
		{
			printf("%s\n", str[i]);
		}
	}
	fclose(stdin);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值