算法习题---5.3字典(Uva10815)

一:题目

给出一段英文,里面包含一些单词,空格和标点,单词不区分大小写,默认都为小写。按照字典序输出这些单词(这些单词不能有重复,字母全部变成小写)

(一)样例输入

Adventures in Disneyland

Two blondes were going to Disneyland when they came to a fork in the
road. The sign read: "Disneyland Left."

So they went home.

(二)样例输出

a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when

二:代码实现

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <set>
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;
set<string> dict;    //单词字典

int main()
{
    freopen("data5_3.in", "r", stdin);
    freopen("data5_3.out", "w", stdout);

    string str;
    while (cin >> str)
    {
        for (int i = 0; i < str.length(); i++)
            if (isalpha(str[i]))
                str[i] = tolower(str[i]);
            else
                str[i] = ' ';
        stringstream ss(str);
        while (ss >> str)
            dict.insert(str);
    }
    //进行打印
    for (set<string>::iterator iter = dict.begin(); iter != dict.end(); iter++)
        cout << *iter << endl;
    freopen("CON", "r", stdin);
    freopen("CON", "w", stdout);
    return 0;
}

(一)stringstream存在sstream头文件中

clear() — to clear the stream  清空字符串流
str() — to get and set string object whose content is present in stream.  得到一个string对象
operator << — add a string to the stringstream object.  通过字符串对象得到一个stringstream对象
operator >> — read something from the stringstream object,  将stringstream对象中的数据输出到其他对象
包含:去除字符串中的空格

(二)字符串函数

函数名称 返回值
isalnum() 如果参数是字母数字,即字母或数字,该函数返回true
isalpha() 如果参数是字母,该函数返回真
isblank() 如果参数是空格或水平制表符,该函数返回true
iscntrl() 如果参数是控制字符,该函数返回true
isdigit() 如果参数是数字(0~9),该函数返回true
isgraph() 如果参数是除空格之外的打印字符,该函数返回true
islower() 如果参数是小写字母,该函数返回true
isprint() 如果参数是打印字符(包括空格),该函数返回true
ispunct() 如果参数是标点符号,该函数返回true
isspace()

如果参数是标准空白字符,如空格、进纸、换行符、回车

、水平制表符或者垂直制表符,该函数返回true

isupper() 如果参数是大写字母,该函数返回true
isxdigit() 如果参数是十六进制的数字,即0~9、a~f、A~F,该函数返回true
tolower() 如果参数是大写字符,则返回其小写,否则返回该参数
toupper() 如果参数是小写字母,则返回其大写,否则返回该参数

猜你喜欢

转载自www.cnblogs.com/ssyfj/p/11512478.html
今日推荐