title: Creating a Function in my C Program? - Getting Statistics for 2 Text Files
body: <p>I was wondering how would I be able to narrow this down so that I don't have to have so many pieces of code repeated? I'm not so sure on how to create functions and call upon them in the main program.</p>
  
  <pre><code>#define _CRT_SECURE_NO_WARNINGS
  
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  void main() {
  FILE *openFile;
  char fileName1[1500] = "", fileName2[1500] = "";
  char character;
  int lineCount, wordCount, charCount;
  
  printf("Enter the first filename: ");
  gets(fileName1);
  
  openFile = fopen(fileName1, "r");
  
  lineCount = 0;
  wordCount = 0;
  charCount = 0;
  
  if (openFile)   {
  
      while ((character = getc(openFile)) != EOF)
      {
  
          if (character != ' ' &amp;&amp; character != '\n')
          {
              ++charCount;
          }
  
          if (character == ' ' || character == '\n')
          {
              ++wordCount;
          }
          if (character == '\n')
          {
              ++lineCount;
          }
      }
      if (charCount &gt; 0)
      {
          ++lineCount;
          ++wordCount;
      }
  }
  
  else
  {
      printf("Failed to open the file\n");
  }
  
      printf("The number of lines : %d \n", lineCount);
      printf("The number of words: %d \n", wordCount);
      printf("The number of characters : %d \n", charCount);
  
  fclose(openFile);
  
  printf("Enter the second filename: ");
      gets(fileName2);
  
      openFile = fopen(fileName2, "r");
  
      lineCount = 0;
      wordCount = 0;
      charCount = 0;
  
      if (openFile)   {
  
          while ((character = getc(openFile)) != EOF)
          {
  
              if (character != ' ' &amp;&amp; character != '\n')
              {
                  ++charCount;
              }
  
              if (character == ' ' || character == '\n')
              {
                  ++wordCount;
              }
              if (character == '\n')
              {
                  ++lineCount;
              }
          }
          if (charCount &gt; 0)
          {
              ++lineCount;
              ++wordCount;
          }
      }
  
      else
      {
          printf("Failed to open the file\n");
      }
  
      printf("The number of lines : %d \n", lineCount);
      printf("The number of words: %d \n", wordCount);
      printf("The number of characters : %d \n", charCount);
  
      fclose(openFile);
  
  getchar();
  }
  </code></pre>