title: How can I reverse the words in a sentence without using built-in functions?
body: <p>This was the interview question: </p>
  
  <blockquote>
    <p>How to convert <strong><code>Dogs like cats</code></strong> to <strong><code>cats like Dogs</code></strong> ?</p>
  </blockquote>
  
  <p>My code shows: <strong><code>cats like cats</code></strong>. Where am I making the mistakes?</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int main()
  {
      char sentence[] = ("dogs like cats");
      cout &lt;&lt; sentence &lt;&lt; endl;
  
      int len = 0;
  
      for (int i = 0; sentence[i] != '\0'; i++)
      {
          len++;
      }
      cout &lt;&lt; len &lt;&lt; endl;
  
      char reverse[len];
      int k = 0;
  
      for (int j = len - 1; j &gt;= 0; j--)
      {
          reverse[k] = sentence[j];
          k++;
      }
  
      cout &lt;&lt; reverse &lt;&lt; endl;
  
      int words = 0;
      char str[len];
  
      for (int l = 0; reverse[l] != '\0'; l++)
      {
          if (reverse[l] == ' ' || reverse[l] == '\0') // not sure about this part
          {
              for (int m = l; m &gt;= 0; m--)
              {
                  str[words] = reverse[m];
                  words++;
              }
          }
      }
  
      cout &lt;&lt; str;
  
      return 0;
  }
  </code></pre>
  
  <p>I know you can do this using pointers, stack, vectors... but interviewer was not interested in that!</p>