char* string2 = string1;
saves the address of the array string1 in the pointer string2 - it does not copy the contents of string1 to string2. Defining and initializing C-strings (b) illustrates the behavior of the assignment operator in conjunction with C-strings.
Although not required by all compilers, you should addchar* string2 = string1;char string2[100]; strcpy(string2, string1);
#include <cstring>
at the program's top. Use strcpy_s(string2, 100, string1) on Windows platforms.
string string2 = string1;
copies the contents of string1 to string2.#include <iostream> #include <cstring> using namespace std; int main() { char word[100]; char line[500] = ""; cout << "Enter a word w/o spaces, press Return to stop: "; cin.getline(word, 100); while (strlen(word) > 0) { strcat(line, word); cin.getline(word, 100); } cout << line << endl; return 0; }Although the program only reads one word at a time, it still uses getline because cin >> word doesn't properly null-terminate the input on all systems. The program uses line as an accumulator, so it must initialize it to an empty string. Use strcat_s(line, 500, word) on Windows platforms.
#include <iostream> #include <string> using namespace std; int main() { string word; string line; cout << "Enter a word w/o spaces, press Return to stop: "; getline(cin, word); while (word.length() > 0) { line += word; getline(cin, word); } cout << line << endl; return 0; }The program still uses line as an accumulator, but the default string constructor makes the string objects empty. The concatenation with the assignment operator, +=, concatenates the two strings and saves the result in line.
#include <iostream> #include <cstring> using namespace std; int main() { char line[100]; cout << "Enter a line of text: "; cin.getline(line, 100); char* word = strtok(line, " "); while (word != nullptr) { cout << word << endl; word = strtok(nullptr, " "); } return 0; } |
#include <iostream> #include <cstring> using namespace std; int main() { char line[100]; cout << "Enter a line of text: "; cin.getline(line, 100); for (char* word = strtok(line, " "); word != nullptr; word = strtok(nullptr, " ")) cout << word << endl; return 0; } |
Enter a line of text: see, the; quick!? red.,; jump over the lazy.... brown dog see the quick red jump over the lazy brown dog
While the text introduced a few string member functions (including find), it suggested that there are too many to cover in detail. To use any programming language effectively and efficiently, we must learn how to use its documentation. So, my approach for string member functions is describing some basic features of many functions and providing a link to my favorite, high-quality online C++ documentation. The following solution will use find and substr functions. Reading the documentation will help you understand the solutions.
The find function quickly locates the space characters in the input, which implies that the most challenging part of the problem is extracting the individual words. The substr function extracts and returns a sub-string from the target or input string. It requires two arguments: the index location where the sub-string begins and the length of the sub-string. As the following picture illustrates, our solution requires two index variables: front and
back saves the result of the most recent call to find, while front is calculated from the previous find call. The difference between the two indexes, back - front
, is the length of the sub-string: 13 - 8 = 5
.
#include <iostream> #include <string> using namespace std; int main() { string line; cout << "Enter a line of text: "; getline(cin, line); size_t front = 0; size_t back = line.find(" "); while (back != string::npos) { cout << line.substr(front, back-front) << endl; front = back + 1; back = line.find(" ", front); } cout << line.substr(front, back) << endl; return 0; } |
#include <iostream> #include <string> using namespace std; int main() { string line; cout << "Enter a line of text: "; getline(cin, line); size_t front = 0; size_t back; do { back = line.find(" ", front); cout << line.substr(front, back-front) << endl; front = back + 1; } while (back != string::npos); return 0; } |
Next, we replace find with find_first_of. The functions' behavior is the same when they search for a single character (their first argument), so we could use find_first_of in the Problem 8 solutions. However, the functions behave differently when their first argument contains multiple characters: find searches for the entire sequence of characters while find_first_of searches for the individual characters.
#include <iostream> #include <string> using namespace std; int main() { string line; cout << "Enter a line of text: "; getline(cin, line); size_t front = 0; size_t back; do { back = line.find_first_of(" .,;:?!", front); if (back - front > 0) cout << line.substr(front, back-front) << endl; front = back + 1; } while (back != string::npos); return 0; }
#include <iostream> #include <cstring> using namespace std; int main() { char line[100]; cout << "Enter a line of text: "; cin.getline(line, 100); char* delimiter; char* next = nullptr; if (line[0] == '"') // a delimiter = "\""; else delimiter = ","; char* field = strtok_s(line, delimiter, &next); // b while (field != nullptr) // c { cout << field << endl; if (next != nullptr && next[0] == '"') // d { next++; // e delimiter = "\""; // f } else delimiter = ","; field = strtok_s(nullptr, delimiter, &next); } return 0; }There are many ways to solve the problem. For example, we could call strtok_s (or strtok_r) in the if-statements rather than setting delimiter. But the first call to strtok_s (highlighted in yellow) requires a different first argument than subsequent calls (highlighted in coral), and this constraint causes some unavoidable complexity in the program. Recall that the strtok_s's third argument, next in this program, saves the current parse location between calls.
"
,"
and"
.#include <iostream> #include <string> using namespace std; int main() { string line; cout << "Enter a line of text: "; getline(cin, line); size_t front = 0; size_t back; do { if (line.at(front) == '"') // a { front++; // b back = line.find_first_of("\"", front); // c } else back = line.find_first_of(",", front); // d if (back - front > 0) cout << line.substr(front, back-front) << endl; // e front = back + 1; // f } while (back != string::npos); // g return 0; }
"
."
."
.,
.