Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

Exercises

23.4

(Pig Latin) Write an application that encodes English language phrases into Pig Latin. Pig Latin is a form of coded language often used for amusement. Many variations exist in the methods used to form Pig Latin phrases. For simplicity, use the following algorithm:

To translate each English word into a Pig Latin word, place the first letter of the English word at the end of the word and add the letters “ay.” Thus, the word “jump” becomes “umpjay,” the word “the” becomes “hetay” and the word “computer” becomes “omputercay.” Blanks between words remain blanks. Assume the following: The English phrase consists of words separated by blanks, there are no punctuation marks and all words have two or more letters. Enable the user to input a sentence. Use a regex_token_iterator to divide the sentence into separate words. Function getPigLatin should translate a single word into Pig Latin.
23.5

(Using Regular Expressions to Convert to Uppercase) Write a program that uses regular expressions to convert the first letter of all words to uppercase. Have it do this for an arbitrary string input by the user.

23.6

(Counting Character Types with Regular Expressions) Use a regular expression to count the number of digits, characters and white-space characters in a string.

23.7

(Searching for Numbers) Write a regular expression that will search a string and match a valid number. A number can have any number of digits, but it can have only digits and a decimal point. The decimal point is optional, but if it appears in the number, there must be only one, and it must have digits on its left and its right. There should be white space or a beginning- or end-of-line character on either side of a valid number. Negative numbers are preceded by a minus sign.

23.8

(Counting HTML Tags) Write a program that will take HTML as input and will output the number of HTML tags in the string. The program should use regular expressions to count the number of elements nested at each level. For example, the HTML:

<p><strong>hi</strong></p>

has a p element (nesting level 0—i.e., not nested in another tag) and a strong element (nesting level 1). For simplicity, use HTML in which none of the elements contain nested elements of the same type—for example, a table element should not contain another table element.

This solution requires a regular expression concept called a back reference to determine the start and end tags of an HTML element. To find these tags, the same word must appear in the start and end tags. A back reference allows you to use a previous match in the expression in another part of the regular expression. When you enclose a portion of a regular expression in parentheses, the match for that subexpression is stored for you. You can then access the result of that expression using the syntax \digit, where digit is a number in the range 1–9. For example, the regular expression

^(7*).*\1$

matches an entire string that starts and ends with one or more 7s. The strings "777abcd777" and "7abcdef7" both match this regular expression. The \1 in the preceding regular expression is a back reference indicating that whatever matched the subexpression (7*) should also appear at the end of the string. The first parenthesized subexpression is back referenced with \1, the second is back referenced with \2, etc.

You’ll need a recursive function so that you can process the nested HTML elements. In each recursive call, you’ll need to pass the contents of an element as the string to be processed in that call—for example, the contents of the p element in this example’s HTML would be

<strong>hi</strong>

Use parentheses to store the content that appears between the start and end tags of a string that matches your regular expression. This value is stored in the match_results object and can be accessed using the [] operator on that object. As with back references, the subexpression matches are indexed from 1 to 9.

23.9

(Removing Extra Spaces) Write a program that asks the user to enter a sentence and uses a regular expression to check whether the sentence contains more than one space between words. If so, the program should remove the extra spaces. For example, the string "Hello World" should be "Hello World".

23.10Answer the following questions about smart pointers:
  1. Describe a situation in which a custom deleter function would be used.

  2. Describe a situation in which you’d use a weak_ptr that is not responsible for lifetime management of its resource.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial