C++ Strings

Translate English to Pig Latin

  1. The substr (“substring”) member function of class string.
    1. substr.C, substr.txt

  2. Another way to break up a string: get input from an object of class istringstream, just like we get input from the object cin.
    1. istringstream.C, istringstream.txt

  3. Use the member function find_first_not_of to find the exact point at which you want to dismember a string.
    1. find_first_not_of.C, find_first_not_of.txt

  4. transform all the values in a container such as a vector or a string.
    1. transform1.C, transform1.txt

  5. It’s convenient to let the last argument of the transform algorithm be a lambda function (an anonymous function).
    1. transform2.C, transform2.txt

  6. Translate English to Pig Latin.
    1. pig.C, pig.txt
    c++ -o ~/bin/pig pig.C
    ls -l ~/bin/pig
    
    pig < newcolossus.txt
    

  7. Exercise.
    If a word begins with “qu”, I would like to move both of these letters to the end of the word.
    For example, “question” should become ESTIONQUAY.
    In pig.C, change
    		//Index of first non-consonant character.
    
    		const string::size_type c
    			{word.find_first_not_of(consonants)};
    
    to
    		//Index of first non-consonant character.
    		string::size_type c {0};
    
    		if (word.substr(0, 2) == "QU") {
    			c = 2;
    		} else {
    			c = word.find_first_not_of(consonants);
    		}
    

  8. Exercise.
    Strip off the leading punctuation as well as the trailing punctuation.
    "Keep, ancient lands, your storied pomp!"

The Markov Chain Algorithm

This example is from The Practice of Programming (1999) by Brian W. Kernighan and Rob Pike.

  1. The shuffle algorithm supersedes the random_shuffle algorithm.
    Each time you run it, you will probably get different output.
    1. shuffle1.C, shuffle1.txt

  2. Shuffle a vector of strings.
    1. shuffle2.C, shuffle2.txt

    The input file is Ernest Hemingway’s novel The Sun Also Rises (1926).
    The “stream editor” sed changes apostrophe to single quote, “quotation marks” to double quotes.
    nroff packs the words of input into lines of output that each contain several words.
    When using more, press space bar or q.

    c++ -o ~/bin/shuffle2 shuffle2.C
    ls -l ~/bin/shuffle2
    
    wget -O - https://gutenberg.org/cache/epub/67138/pg67138.txt | awk 'NR >= 95' | sed $'s/\u2019/\u0027/g;s/\u201C/\u022/g;s/\u201D/\u022/g' > rises.txt
    
    shuffle2 < rises.txt | more    
    
    shuffle2 < rises.txt | nroff | more
    
    shuffle2 < rises.txt | nroff > shuffle2.txt
    

  3. A stack is LIFO (last in, first out): last hired, first fired.
    A queue is FIFO (first in, first out): people standing on line, waiting to be served.
      queue.C, queue.txt

  4. The operator[] member function of class map can add a new pair to the map.
    1. cities.C, cities.txt

  5. Run the Markov chain algorithm.
    A prefix is a pair of two consecutive words of input.
    The suffix is the word of input that comes after them.
    For each prefix, the map holds a vector of all the suffixes that came after occurrences of the prefix in the input.
    1. prefix.h
    2. prefix.C
    3. main.C, main.txt
    c++ -o ~/bin/markov prefix.C main.C
    markov < rises.txt | more                (Press space bar or q) 
    markov < rises.txt | nroff | more        (Press space bar or q)
    markov < rises.txt | nroff > main.txt
    
    rm rises.txt
    

A guessing game that builds a tree

  1. In this binary tree, each internal node holds a yes/no question and has two children.
    Each leaf node holds an animal and has no children.
    We travel down the tree using recursion.
    animal.C, animaldialog.txt, animal.txt

    “Yes” goes to the lower left;
    “No” goes to the lower right.

    Your browser does not support the HTML CANVAS tag.

Eliza, the first Chatbot (1966)

Homeworks

  1. languages.cgi languages.C by jc208
  2. bengli.cgi bengali.C by kwesner1. Must use wide wcout, not plain old cout.
  3. CtoF.html CtoF.C by mrd22. Try −40° Celsius.
  4. weightagain.html weightagain.C. by ljl8.
    Change the vector<body> to a vector<pair<string, double>>.
    #include <string>
    #include <utility>
    #include <vector>
    
    	const vector<pair<string, double>> aduetog {
        		{"",         0.0},
    		{"Mercury", 0.38},
        		{"Venus",   0.91},
        		{"Earth",   1.00},
        		{"Mars",    0.38},
        		{"Jupiter", 2.34},
        		{"Saturn",  1.06},
        		{"Uranus",  0.92},
       		{"Neptune", 1.19}
    	};
    
    	if (planet < 1 || planet >= aduetog.size()) {   //Don't count them yourself.
    		error message;
    	}
    
    	const auto& p {vector[planet]};    //p is a refernce to a pair<string, double>
    	const double weightcalc {weight * p.second};
    
    	cout << "<BR/"> << p.first << " has " << p.second
    		<< " times as much gravity as the Earth.\n";
    
    	cout << "<BR/>Your weight on " << p.first << " is " << weightcalc << " pounds.\n";
    
  5. magnolia.html magnolia.C by jmm56.
    You can say
    	string base;       //The constructor with no arguments puts "" into the string.
    
    instead of
    	string base {""};  //Call the constructor with one argument.
    
  6. latin.cgi Latin.C by an151. See the Unicode code chart.