Note that there are some explanatory texts on larger screens.

plurals
  1. PODetecting string argument in a loop
    primarykey
    data
    text
    <p>The program takes a string using getline, and then passes that string to a function where it stores the string into substrings separated by whitespace. I did that just by reading characters with a loop.</p> <p>However, now I'm trying to pass a second string argument that separates the strings into substrings if the loop encounters characters in the 2nd string argument. This is what I have so far.</p> <pre><code>#include "std_lib_facilities.h" vector&lt;string&gt; split(const string&amp; s, const string&amp; w) // w is second argument { vector&lt;string&gt; words; string altered; for(int i = 0; i &lt; s.length(); ++i) { altered+=s[i]; if(i == (s.length()-1)) words.push_back(altered); else if(s[i] == ' ') { words.push_back(altered); altered = ""; } } return words; } int main() { vector&lt;string&gt; words; cout &lt;&lt; "Enter words.\n"; string word; getline(cin,word); words = split(word, "aeiou"); // this for example would make the letters a, e, i, o, // and u divide the string for(int i = 0; i &lt; words.size(); ++i) cout &lt;&lt; words[i]; cout &lt;&lt; endl; keep_window_open(); } </code></pre> <p>However, obviously I can't do something like</p> <pre><code>if(s[i] == w) </code></pre> <p>because s[i] is a char and w is a string. Do I need to use a stringstream to parse the string instead of the loop I implemented? I actually played around with stringstream, but don't really know how it could help because either way I have to read the characters 1 by 1.</p> <p>P.S. The arguments to split must be passed as strings, and the input form in main() must be a getline.</p>
    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.
 

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