title: How to find the middle 3 characters of a word with an odd number of characters using substring? (C++)
body: <p>This is my assignment:</p>
  <blockquote>
  <p>The midfix of 3 is the middle 3 characters of a string. Given a string input, output the middle three characters of that string. Assume the word length is always odd and at least three characters.</p>
  <p>Hint: Use the string function substr().</p>
  <p>Ex: If the input is:</p>
  <p>xxxtoyxxx</p>
  <p>the output is:</p>
  <p>Midfix: toy</p>
  </blockquote>
  <p>I am unable to get my code to fit for any word with an odd word length (i.e. the code works for <code>flags</code> (<code>lag</code>) but not for <code>xxxtoyxxx</code> or <code>cat</code>. I know how to find characters on a specific word, but do not know how to make it work for any odd word length word.</p>
  <p>This is my code so far:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  int main() 
  {
     string a;
  
     cin &gt;&gt; a;
  
     int length = a.length();
  
     int middle = length/2;
  
     string c = a.substr(middle-1, middle+1);
  
     cout &lt;&lt; &quot;Midfix: &quot; &lt;&lt; c &lt;&lt; endl;
  
     return 0;
  }
  </code></pre>