Algorithms Exercises --- 5.3 Dictionary (Uva10815)

A: Title

Given some English, which contains some words, spaces and punctuation, not case-sensitive by default all lowercase. These words according to the dictionary order output (a word which can not be duplicated, all lowercase letters)

(A) Sample Input

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.

(B) Sample Output

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

Two: code implementation

#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;
}

(A) the presence of the stringstream sstream header files

clear () - to clear the stream flow empty string 
str () -. to get and set string object whose content is present in stream to obtain a string object 
operator << -. add a string to the stringstream object obtained by the object string a stringstream Object 
operator >> - read something from the stringstream object, the output data stringstream object to other objects
Includes: removing spaces in the string

(B) String Functions

Function name return value
isalnum () If the parameter is alphanumeric letters or numbers i.e., the function returns true
Islf () If the parameter is the letter, the function returns true
isblank() If the parameter is a space or horizontal tab, the function returns true
iscntrl() If the argument is a control character, the function returns true
isdigit () If the parameter is a number (0 to 9), the function returns true
isgraph() If the argument is a printing character other than a space, the function returns true
islower() If the parameter is a lowercase letter, the function returns true
Printing () If the argument is a printable characters (including spaces), the function returns true
ispunct() If the argument is a punctuation mark, the function returns true
isspace()

If the parameter is a standard blank characters, such as space, feed, line feed, carriage return

, Horizontal or vertical tab tab, the function returns true

isupper() If the argument is an uppercase letter, the function returns true
isxdigit() If the parameter is a hexadecimal number, i.e., 0 ~ 9, a ~ f, A ~ F, the function returns true
tolower() If the argument is uppercase, lowercase it is returned, otherwise the argument
toupper() If the argument is a lowercase letter, whose capital is returned, otherwise the argument

Guess you like

Origin www.cnblogs.com/ssyfj/p/11512478.html
5.3