title: Splitting a sentence and retrieving a specific word
body: <p>I've managed to write the following piece of code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  using namespace std;    
  int main()
  {
      string sentence;
      getline(cin, sentence);
      char* ptr = &amp;sentence[0];
      while ( *ptr != '\0' ){
          if ( *ptr == ' ' ){
              cout &lt;&lt; endl;
          }
          else{
              cout &lt;&lt; *ptr;
          }
          ptr++;
      }
  }
  </code></pre>
  
  <p>By using that I can print each word of a sentence separately. However I want to store them and then retrieve them. Here's a sample run:</p>
  
  <pre><code>Enter the sentence:This is a sample sentence.
  Which word do you want to see ?:4
  sample
  </code></pre>
  
  <p>I don't know how to proceed from my code above. I thought storing each letter in a char array, then convert these arrays to strings and store them in a <code>vector&lt;string&gt;</code> but couldn't figure it out.</p>
  
  <p>I would like to do it with only using given libraries and without using any splitting functions if possible.</p>
  
  <p><strong>EDIT:This is what i've tried lately.Doesn't work though.</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  using namespace std;
  int main()
  {
      char letter;
      vector&lt;string&gt; words;
      vector&lt;char&gt; temp;
      vector&lt;char&gt; sentence;
      while( cin &gt;&gt; letter ){   // ctrl-z to break
          sentence.push_back(letter);
      }
      char* ptr = &amp;sentence[0];
      while ( *ptr != '\0'){
          while ( *ptr != ' ' ){
              temp.push_back(*ptr);
              ptr++;
          }
          words.push_back(str(temp));
      }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>EDIT2: Here's a solution with sstream</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  #include &lt;vector&gt;
  using namespace std;
  int main()
  {
      cout &lt;&lt; "Sentence: " &lt;&lt; endl;
      string sentence;
      getline(cin, sentence);
      istringstream sin(sentence);
      vector&lt;string&gt; tokens;
      string word;
      while (!sin.eof()){
          sin &gt;&gt; word;
          tokens.push_back(word);
      }
      cout &lt;&lt; "Which word ?: " &lt;&lt; endl;
      int n;
      cin &gt;&gt; n;
      cout &lt;&lt; tokens[n - 1] &lt;&lt; endl;
  }
  </code></pre>
  
  <p><strong>EDIT3: Rite, got it.Here's the solution i wanted.</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  int wordbyword(string sentence, char** words)
  {
      int i = 0, j = 0, k = 0;
      while (sentence[i] != '\0'){
          if (sentence[i] != ' '){
              words[j][k] = sentence[i];
              k++;
          }
          else {
              j++;
              k = 0;
          }
          i++; 
  
      }
      return j;   
  }
  
      int main()
      {
          string sentence;
          cout &lt;&lt; "Sentence: "&lt;&lt; endl;
          getline(cin, sentence);
          int size = sentence.length();
          char** words = new char*[size];
          for ( int i = 0; i &lt; size; i++)
              words[i] = new char[size];
  
          int wordCount = wordbyword(sentence, words) + 1;    
          while(1){
              cout &lt;&lt; "Word number: " &lt;&lt; endl;
              int n;
              cin &gt;&gt; n;
              if ( n == 0){
                  cout &lt;&lt; "Terminating..." &lt;&lt; endl;
                  break;
              }
              else if ( n &gt; wordCount || n &lt; 0)
                  cout &lt;&lt; "Word doesn't exist" &lt;&lt; endl;
              else
                  cout &lt;&lt; words[n - 1] &lt;&lt; endl;
              }
  
      }
  </code></pre>