title: Comparison of strings for Mastermind game
body: <p>I am writing a program that simulates the game Mastermind, but I am struggling with how to compare guessed pattern to key pattern.
  The game conditions are a little bit changed:  </p>
  
  <ul>
  <li>patterns consist of letters.  </li>
  <li>if an element of guessed pattern is equal to element of key pattern, and also index is equal, then print b.  </li>
  <li>if an element of guessed pattern is equal to element of key pattern, but index is not, then print w.  </li>
  <li>if an element of guessed pattern is not equal to element of key pattern, print dot.  </li>
  <li>in feedback about guessed pattern, 'b's come first, 'w's second, '.'s last.  </li>
  </ul>
  
  <h3>Original key vs guess pattern match code</h3>
  
  <pre><code>    for (i=0; i&lt;patternlength; i++)
      {
          for (x=0; x&lt;patternlength; x++)
          {
             if (guess[i]==key[x] &amp;&amp; i==x)
                  printf("b");
              if (guess[i]==key[x] &amp;&amp; i!=x)
                  printf("w");
              if (guess[i]!=key[x])
                  printf(".");
          }   
      }
  </code></pre>
  
  <h3>Revised code</h3>
  
  <p>This is using some of the answer provided by <a href="https://stackoverflow.com/users/15168/jonathan-leffler">Jonathan Leffler</a>.  Unfotunately, it isn't working correctly yet; can you help me?</p>
  
  <p>The functions length() and guessnum() are defined already.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  int length()
  {
    int length;
    printf("Enter the pattern length: ");
    scanf("%d", &amp;length);
    return length;
  }
  int guessnum()
  {
    int guessnum;
    printf("Enter the number of guesses: ");
    scanf("%d", &amp;guessnum);
    return guessnum;
  }
  int main(void)
  {
    int patternlength = length();
    char key[patternlength+1];      
    char keyc[patternlength+1];    
    int numguess = guessnum();
    char guess[patternlength+1];    
    printf("Input the key pattern with no spaces: ");
    scanf("%s", key);
    int i,j,count = 1;
    int bcount = 0, wcount = 0;
    char guessc[patternlength+1];
    guessc[0] = '\0';            
    int ind;
    char output[patternlength];
    for (ind=0; ind&lt;(patternlength+1); ind++)
      output[ind]='\0';
    char outputc[patternlength+1];
    char guessold[patternlength+1];
    for (ind=0; ind&lt;(patternlength+1); ind++)
    guessold[ind]='\0';
    while (strcmp(key, guess) !=0 &amp;&amp; count&lt;=numguess)
      {
        if(count&gt;1)
      strcpy(guessold, guess);
        strcpy(keyc, key);          
        printf("Input a guess pattern with no spaces: ");
        scanf("%s", guess);
        if (count&gt;1)
      printf("%d: %s %s\n", count-1, output, guessold);
        strcpy(guessc, guess);
        wcount = 0;    
        bcount = 0;     
        printf("%d: ", count);
        for (i = 0; i &lt; patternlength; i++)
      {
        if (keyc[i] == guessc[i])
          {
            putchar('b');
            keyc[i] = guessc[i] = '.';
            bcount++;
            for (ind=0; ind&lt;patternlength; ind++)
          output[ind]='b';
          }
      }
        if (bcount != patternlength)
      {
        for (i = 0; i &lt; patternlength; i++)
          {
            if (guessc[i] != '.')
          {
  
            for (j = 0; j &lt; patternlength; j++)
              {
  
                if (guessc[i] == keyc[j])
              {
                wcount++;
                putchar('w');
                for (ind=0; ind&lt;patternlength; ind++)
                  if (output[ind]!='b')
                    output[ind]='w';
  
                keyc[j] = guessc[i] = '.';
                break;
              }
              }
          }
          }
        for (i = bcount  +  wcount; i &lt; patternlength; i++)
          putchar('.');
        for (ind=bcount+wcount; ind&lt;patternlength; ind++)
          output[ind]='.';
      }
        count++;
        printf(" %s\n", guess);
        strcpy(outputc, output);
      }
    if (strcmp(key, guess) != 0)
      {
        printf("You did not guess the pattern!\n");
      }
    else 
      {
        printf("You guessed the pattern!\n");
      }
    return 0;
  }
  </code></pre>
  
  <p>output of code above:</p>
  
  <pre><code>Enter the pattern length: 3
  Enter the number of guesses: 3
  Input the key pattern with no spaces: abc
  Input a guess pattern with no spaces: acb
  1: bww acb
  Input a guess pattern with no spaces: abb
  1: bbb acb
  2: bb. abb
  Input a guess pattern with no spaces: abc
  2: bb. abb
  3: bbb abc
  You guessed the pattern!
  </code></pre>
  
  <p><strong><em>required output:</em></strong></p>
  
  <pre><code>    Enter the pattern length: 3
      Enter the number of guesses: 3
      Input the key pattern with no spaces: abc
      Input a guess pattern with no spaces: acb
      1: bww acb
      Input a guess pattern with no spaces: abb
      1: bww acb
      2: bb. abb
      Input a guess pattern with no spaces: abc
      1: bww acb
      2: bb. abb
      3: bbb abc
      You guessed the pattern!
  </code></pre>
  
  <p>I tried to use one more string, which will store in it feedback of the guess, but when there are several guesses, i think i should use some kind of loop to print feedback of all previous guesses each time new guess is made. but it is difficult to me figure out how should i write this loop with the structure suggested by <a href="https://stackoverflow.com/users/15168/jonathan-leffler">Jonathan Leffler</a>.</p>
  
  <p>I added my last correction to the code, so I almost reached the desired output. does anyone have an idea what is possible to do here?</p>