讀取帶標點的英文段落,分詞、去重按字典順序輸出(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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章