题意是找出一串字符串里面的单词,并按字典序排序。
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char s[50005][205];
bool isletter(char &c)
{
if (c >= 'a' && c <= 'z')
return true;
if (c >= 'A' && c <= 'Z')
{
c += 32;//&c 引用才能改变大小写字母
return true;
}
return false;
}
int cmp(const void *va, const void *vb)
{
char *a = (char*)va;
char *b = (char*)vb;
return strcmp(a, b);
}
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
char tmp;
int i = 0, j = 0;
bool flag = false;
while ((tmp = getchar()) != EOF)
{
if (isletter(tmp) && !flag)
{
i = 0;
s[j][i++] = tmp;
flag = true;
}
else if (isletter(tmp) && flag)
{
s[j][i++] = tmp;
}
else if (!isletter(tmp) && flag)
{
flag = false;
s[j][i] = '\0';
j++;
}
}
qsort(s, j, sizeof (s[0]), cmp);
for (i = 0; i < j - 1; i++)
{
if (strcmp(s[i], s[i + 1]) == 0)
continue;
printf("%s\n", s[i]);
}
printf("%s\n", s[i]);
return 0;
}