思路:读入字符串,判断是否已经有相同的字符串,有则跳过,否则,添加到列表中。之后进行字符串的排序,最后输出。其实和数字完全一个道理。
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char s[110][55] =
{ };
int i, j;
int n;
n = 0;
char ts[110] =
{ };
while (cin >> ts)
{
for (i = 0; i < n; i++)
{
if (!strcmp(ts, s[i]))
break;
}
if (i == n)
{
strcpy(s[n], ts);
n++;
}
}
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1 - i; j++)
{
if (strcmp(s[j], s[j + 1]) > 0)
{
char t[110] =
{ };
strcpy(t, s[j]);
strcpy(s[j], s[j + 1]);
strcpy(s[j + 1], t);
}
}
}
for (int i = 0; i < n; i++)
{
cout << s[i] << endl;
}
return 0;
}