title: Capitalization of a String
body: <p>I'm extremely new to C++ and I have an assignment to first format a name provided via the user, and then ensure it has proper capitalization (First letter capital, rest lowercase). </p>
  
  <p>I feel like I have the function correct, but the function involves an array and the code I have written does not seem to allow a string to be initialized to the array.</p>
  
  <p>The output of the first stretch of code is then input into the capitalize function and that is where I am running into an error.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;cstring&gt;
  using namespace std;
  
  void capitalize(char str[]);
  
  int main()
  {
      //inputs user name in the format first, middle and last name
      string name;
      cout &lt;&lt; "Enter your name in the order of first middle last: ";
      getline(cin, name, '\n');
  
      //string variables to extract the first. middle and last names
      //from the input
      string first, last, middle, subOutput;
  
  
  
      // finds the poisiton of first space
      int firstblank = name.find(' ', 0);
  
  
      // taken as first name until the first space
      first = name.substr(0, firstblank);
  
  
      // finds second space
      int secondblank = name.find(' ', firstblank + 1);
  
  
      // if second space is not found means no middle name in the input
      if (secondblank == -1)
      {
          // taken the remaining string as last name
          last = name.substr(firstblank + 1);
          // prepares name as desired output
          subOutput = last + ", " + first;
      }
      else
      {
          // gets middle name from firstspace to second space
          middle = name.substr(firstblank + 1, secondblank - firstblank - 1);
          // gets last name
          last = name.substr(secondblank + 1);
          //prepares output
          subOutput = last + ", " + first + " " + middle.at(0) + ".";
      }
  
      char O[] = subOutput;
      capitalize(O);
      cout &lt;&lt; O &lt;&lt; endl;
  
  
      //displays output
      //cout &lt;&lt; O &lt;&lt; endl;
  
      return 0;
  }
  
  void capitalize(char str[]) {
      if (strlen(str) == 0) {
          return;
      }
      str[0] = toupper(str[0]);
      for (int i = 1; i &lt; strlen(str); i++)
      {
          str[1] = tolower(str[i]);
      }
  }
  
  </code></pre>
  
  <p>Might be a very basic fix but like I said, I am very new to coding/C++ so any help is greatly appreciated!</p>
  
  <p>EDIT: 
  I actually revised my code and I believe I have some of the solution, however
  there is a bug that I am unsure about. 
  New Code: </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;cstring&gt;
  using namespace std;
  
  const int CAPACITY = 1000;
  
  
  void capitalize(string&amp; str);
  
  
  int main()
  {
      //inputs user name in the format first, middle and last name
      //string variables to extract the first. middle and last names
      string name, first, last, middle, subOutput, Output;
      cout &lt;&lt; "Enter your name in the order of first middle last: ";
      getline(cin, name, '\n');
  
  
      //from the input
  
  
  
  
      // finds the poisiton of first space
      int firstblank = name.find(' ', 0);
  
  
      // taken as first name until the first space
      first = name.substr(0, firstblank);
  
  
      // finds second space
      int secondblank = name.find(' ', firstblank + 1);
  
  
      // if second space is not found means no middle name in the input
      if (secondblank == -1)
      {
          // taken the remaining string as last name
          last = name.substr(firstblank + 1);
          // prepares name as desired output
          subOutput = last + ", " + first;
      }
      else
      {
          // gets middle name from firstspace to second space
          middle = name.substr(firstblank + 1, secondblank - firstblank - 1);
          // gets last name
          last = name.substr(secondblank + 1);
          //prepares output
          subOutput = last + ", " + first + " " + middle.at(0) + ".";
      }
  
  
      capitalize(subOutput);
      Output = subOutput;
      cout &lt;&lt; Output &lt;&lt; endl;
  
  
  
      return 0;
  }
  
  
  
  void capitalize(string&amp; str) {
      if (str.length() == 0) {
          return;
      }
      str[0] = toupper(str[0]);
      for (int i = 1; i &lt; str.length(); i++)
      {
          str[i] = tolower(str[i]);
      }
  }
  </code></pre>
  
  <p>The code compiles and formats properly, however when capitalizing, it only properly capitalizes the last name properly. I am not sure how I can get it to capitalize everything though. Like I said, any help is greatly appreciated!</p>