8.9. Practice Problems

The following problems, organized in three groups, explore C-strings and the string class. The first group requires you to read C++ code and understand the related concepts and how it behaves. The problems in the second group ask you to perform a series of "meaningless" operations (i.e., the problems are not realistic) to help provide you with some experience with the string functions. The last two problems in the final group are more authentic and challenging. Before attempting these problems, you might find it helpful to complete the Chapter 8 Study Guide and answers in the Self-Testing section. A link to the practice problem solutions is at the page's end.
  1. Explain why the following program produces the illustrated output.
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char string1[100];
    	cout << "Enter a short line of text: ";
    	cin.getline(string1, 100);
    
    	char* string2 = string1;
    	cout << "Enter a different line of text: ";
    	cin.getline(string1, 100);
    
    	cout << string1 << endl;
    	cout << string2 << endl;
    
    	return 0;
    }
    Output:
    Enter a short line of text: See the quick red fox
    Enter a different line of text: Hello world
    Hello world
    Hello world
  2. Correct the problem with the code in the previous question.
  3. Explain why the following program produces the illustrated output.
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string string1;
    	cout << "Enter a short line of text: ";
    	getline(cin, string1);
    
    	string string2 = string1;
    	cout << "Enter a different line of text: ";
    	getline(cin, string1);
    
    	cout << string1 << endl;
    	cout << string2 << endl;
    
    	return 0;
    }
    Output:
    Enter a short line of text: See the quick red fox
    Enter a different line of text: Hello world
    Hello world
    See the quick red fox
  4. word and line are C-string variables. Write a program that reads words from the console one at a time into word and concatenates them together in line. The program should loop, reading words and concatenating them until the user enters an empty word by pressing Enter without entering a word. When the program stops looping, it should write line to the console and terminate.
  5. Solve the previous problem but make word and line instances of the string class.
  6. Write a program that reads a line containing spaces from the console, saves it in a C-string, and prints the individual space-separated words to the screen. For example:
    Enter a line of text: see the quick red fox jump over the lazy brown dog
    see
    the
    quick
    red
    fox
    jump
    over
    the
    lazy
    brown
    dog
    Hint: see Parsing: strtok.
  7. Change Problem 6 allowing spaces and the common punctuation characters .,;:?! to separate the words.
  8. Solve Problem 6 but use instances of the string class in place of C-strings. Carefully study string Class Functions.
  9. Change Problem 8 allowing spaces and the common punctuation characters .,;:?! to separate the words.

  10. Comma-separated values (CSV) is a "standard" file format that organizes related data as lines with the individual data items separated by commas. For example, spreadsheet programs can typically export their rows and columns as one line per row, with the columns of each row separated by commas. Similarly, databases export their records with one line per record with the fields separated by commas. Users can exchange data between different programs by exporting and importing it with a CSV file. Suppose that a line represents a spreadsheet row or a database record. If a person has a name, a phone number, and an address, the line might look like this:

    Cranston Q. Snort,801-555-1234,115 Elm Street
    Notice that spaces are significant, so they're not allowed before or after the commas.

    Modifying the solutions for Problems 6 and 8 to parse basic CSV-formatted lines is straightforward. But the CSV format also allows us to "hide" commas between double quotation marks. For example, in the U.S., it's common to write last-name first and first-name last, separated by a comma:

    "Snort, Cranston Q.",801-555-1234,115 Elm Street
    The comma separating "Snort" and "Cranston" doesn't separate values; it is part of a person's name.

    There are many variations on the CSV format, and additional complexity is possible. For example, most systems allow adjacent commas to represent empty fields, and some systems allow users to embed double quotation marks in data by doubling them: "". However, the description presented here sufficiently expresses some realistic and challenging problems.

  11. Write a C++ program that reads a CSV-formatted line from the console into a C-string. The program parses the input and writes each field to the screen. Test your program with the following test cases: Hint: Study Parsing: strtok carefully. Focus specifically on strtok_s and strtok_r. I don't have a Mac for testing, but I think strtok_r will also work on macOS.
  12. Solve problem 10 but use instances of the string class and its member functions. Use the same three test cases.


Practice Problem Answers