#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 |
#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 |
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 dogHint: see Parsing: strtok.
.,;:?!
to separate the words..,;:?!
to separate the words.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 StreetNotice 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 StreetThe 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.
Cranston Q. Snort,801-555-1234,115 Elm Street
"Snort, Cranston Q.",801-555-1234,115 Elm Street
123,"Snort, Cranston Q.",801-555-1234,115 Elm Street
(maybe 123 is an ID or a key)