title: circular array implementation
body: <p>This is my implementation of a circular array so far. It is supposed to store the last 5 commands entered, by entering the 6th command in the place of the 5th and discarding the 1st. What I have managed to do so far is, to able to store the 5 commands and print them out. On the 6th command, I noticed that it goes in the 2nd position (<code>k=1</code>) of the <code>historyArray</code>, but when debugging, <code>k</code> was equal to <code>0</code> which would at least push the last command at the top. If you can put me in the right track again, I would appreciate it. Here is part of the code.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int main (int argc, char *argv[])
  {
      int i=0; 
      int j=0; 
      int k=0;
      int tempIndex = 0;
      int elementCounter = 0;
  
      char inputString[100];
      char *result=NULL;
      char delims[] = " ";
      char historyArray[5][20] = {0};
      char tokenArray[20][20] ;
      char hCommand[1][20];
  
      do
      {
           j = 0;
  
           printf("hshell&gt;");
           gets(inputString);
  
           //skip writing "history" in historyArray
           if (strcmp(inputString,"history")!= 0)
           {
               strcpy (historyArray[k], inputString);
           }
  
           k = (k+1) % 5;
           if (elementCounter &lt;= 5)
               elementCounter++;
  
           // Break the string into parts
           result = strtok(inputString, delims);
  
           while (result!=NULL)
           {
               strcpy(tokenArray[j], result);
               j++;
               result= strtok(NULL, delims);                  
           }
  
           if (strcmp(tokenArray[0], "exit") == 0)
               return 0;
  
           if (strcmp(tokenArray[0], "history") ==  0)
           {
               if (j&gt;1)
               {
                   tempIndex = atoi(tokenArray[j]);
                   puts(tempIndex);
               }
               else
               {
                   for (i=0; i&lt;elementCounter-1;i++)
                       printf("%i. %s\n", i+1, historyArray[i]);
               }
           }
           else
           {
               printf("Command not found\n");
           }
      } while (1);
  }
  </code></pre>
  
  <p>After suggestions (still incomplete):</p>
  
  <pre><code>         j = 0;
           //elementCounter = 0;
           printf("327&gt;");
           gets(inputString);
  
           strcpy (historyArray[k], inputString);
           k = (k+1) % 5;
  
          if (elementCounter &lt;= 5)
           {         
            elementCounter++;                
           }
  </code></pre>