Andy's First Dictionary
大意:
将一段话中的所有单词都找出来,并且按照字典序以小写形式输出
要点:
一个字母也算一个单词 注意第一个单词和最后一个单词的判断
代码:
#include <iostream>
#include <string>
using namespace std;
string word[5005];
string line;
string temp;
int num = 0;
void fun()
{
for (int i = 0; i < num; i++)
{
if (word[i] == temp)
return;
}
if (temp != "")
{
word[num] = temp;
num++;
}
}
void deal(int l)
{
for (int i = 0; i < line.length(); i++)
if (line[i] >= 'A' && line[i] <= 'Z')
line[i] += 32;
for (int i = 0; i < line.length(); i++)
{
if (line[i] >= 'a' && line[i] <= 'z')
temp += line[i];
else
{
fun();
temp = "";
}
if (i == line.length() - 1)
{
fun();
temp = "";
}
}
}
void mysort()
{
for (int i = 0; i < num; i++)
{
string min = word[i];
for (int j = i + 1; j < num; j++)
{
if (min > word[j])
{
min = word[j];
word[j] = word[i];
word[i] = min;
}
}
}
}
int main()
{
while (getline(cin, line))
{
deal(line.length());
}
mysort();
for (int i = 0; i < num; i++)
cout << word[i] << endl;
}