comparision of strings

Posted by EmiLazE on Stack Overflow See other posts from Stack Overflow or by EmiLazE
Published on 2012-11-03T04:53:42Z Indexed on 2012/11/03 5:00 UTC
Read the original article Hit count: 96

Filed under:
|
|

i am writing a program, that simulates game mastermind. but i am struggling on how to compare guessed pattern to key pattern. the game conditions are a little bit changed:
patterns consist of letters.
if an element of guessed pattern is equal to element of key pattern, and also index is equal, then print b.
if an element of guessed pattern is equal to element of key pattern, but index is not, then print w.
if an element of guessed pattern is not equal to element of key pattern, print dot.
in feedback about guessed pattern, 'b's come first, 'w's second, '.'s last.
my problem is that i cannot think of a way totally satisfies the answer.

    for (i=0; i<patternlength; i++)
  {
    for (x=0; x<patternlength; x++)
      {
    if (guess[i]==key[x] && i==x)
      printf("b");
    if (guess[i]==key[x] && i!=x)
      printf("w");
    if (guess[i]!=key[x])
      printf(".");
      }
  }

© Stack Overflow or respective owner

Related posts about c

    Related posts about string