Advertisement
Guest User

afds

a guest
Aug 21st, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. static int match_letters(const char* word1, char* word2, int c)
  5. {
  6.         char *p1 = word1;
  7.         char *p2 = word2;
  8.         int match = 0;
  9.  
  10.         /* find all the occurence of char 'c' in word1 */
  11.         while (*p1) {
  12.                 if (*p1 == (char)c) {
  13.                         *p2 = (char)c;
  14.                         match++;
  15.                 }
  16.                 p1++;
  17.                 p2++;
  18.         }
  19.  
  20.         return match;
  21. }
  22.  
  23. int main(void)
  24. {
  25.         static const char sample_word[] = "facebook";
  26.         static char input_word[sizeof sample_word] = { 0 };
  27.         int c;
  28.         int tries = 0;
  29.         int match = 0;
  30.         int m;
  31.  
  32.         /* just fill in our input word with asterisk */
  33.         for (c = 0; c < sizeof sample_word; c++) {
  34.                 input_word[c] = '*';
  35.  
  36.         }
  37.  
  38.         while (tries < 3) {
  39.                 printf("\nword is: %s\n", input_word);
  40.                 printf("enter a letter: ");
  41.                 c = getchar();
  42.                 while (getchar() != '\n');
  43.  
  44.                 if (m = match_letters(sample_word, input_word, c)) {
  45.                         match += m;
  46.  
  47.                         printf("match = %d, m = %d\n", match, m);
  48.                         if (match == strlen(sample_word)) {
  49.                                 printf("done.\n");
  50.                                 break;
  51.                         }
  52.                         continue;
  53.                 }
  54.  
  55.                 tries++;
  56.                 printf("number of tries left: %d\n", tries);
  57.         }
  58.  
  59.         return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement