Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, I think this is what you want:</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; #include &lt;boost/algorithm/string.hpp&gt; using namespace std; int main() { // Read a line from stdin string line; getline(cin, line); vector&lt;string&gt; str_vec; boost::split(str_vec, line, boost::is_any_of(" ,")); if (str_vec.size() == 3) { string last = str_vec[0]; string first = str_vec[1]; string middle = str_vec[2]; std::cout &lt;&lt; "First=" &lt;&lt; first &lt;&lt; " Last=" &lt;&lt; last &lt;&lt; " Middle=" &lt;&lt; middle &lt;&lt; endl; } } </code></pre> <p>If you don't want to use (or don't have) boost libraries, you can check the below version instead. The comments should help you understand each block of the code.</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; using namespace std; int main() { // Read a line from stdin string line; getline(cin, line); // Erase all commas while (true) { size_t comma_pos = line.find(","); if (comma_pos != std::string::npos) { line.erase(comma_pos, 1); line.insert(comma_pos, " "); } else break; } // Change all two spaces to only one space while (true) { size_t space_pos = line.find(" "); if (space_pos != std::string::npos) line.erase(space_pos, 2); else break; } // Tokenize the names size_t end_last_pos = line.find(" "); if (end_last_pos == string::npos) { cout &lt;&lt; "Missing last name" &lt;&lt; endl; return 1; } string last = line.substr(0, end_last_pos); size_t end_first_pos = line.find(" ", end_last_pos + 1); if (end_first_pos == string::npos) { cout &lt;&lt; "Missing first name" &lt;&lt; endl; return 1; } string first = line.substr(end_last_pos + 1, end_first_pos - end_last_pos - 1); size_t end_middle_pos = line.find(" ", end_first_pos + 1); string middle; if (end_middle_pos == string::npos) middle = line.substr(end_first_pos + 1); else middle = line.substr(end_first_pos + 1, end_middle_pos - end_first_pos - 1); // Print the names std::cout &lt;&lt; "First='" &lt;&lt; first &lt;&lt; "' Last='" &lt;&lt; last &lt;&lt; "' Middle='" &lt;&lt; middle &lt;&lt; "'" &lt;&lt; endl; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload