Itachi Uchiwa
Itachi Uchiwa

Reputation: 3164

C++ primer 5th Ed: The Word Transformation Program

On chapter 11 from C++ Primer 5 Edition, the title "The Word Transformation Program" it has a function called word_transform() which is defined this way:

void word_transform(ifstream &map_file, ifstream &input)
{
    auto trans_map = buildMap(map_file); // store the transformations
    string text; // hold each line from the input
    while (getline(input, text)) { // read a line of input
        istringstream stream(text); // read each word
        string word;
        bool firstword = true; // controls whether a space is
        printed
            while (stream >> word) {
                if (firstword)
                    firstword = false;
                else
                    cout << " "; // print a space between words
                                 // transform returns its first argument or its transformation
                cout << transform(word, trans_map); // print the output
            }
        cout << endl; // done with this line of input
    }
}

Upvotes: 1

Views: 115

Answers (1)

Paul Evans
Paul Evans

Reputation: 27577

Sure, your way prints all the transform(word, trans_map) separated by spaces. But you also print a redundant space at the end, which may or may not be of concern.

Upvotes: 2

Related Questions