Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- static int match_letters(const char* word1, char* word2, int c)
- {
- char *p1 = word1;
- char *p2 = word2;
- int match = 0;
- /* find all the occurence of char 'c' in word1 */
- while (*p1) {
- if (*p1 == (char)c) {
- *p2 = (char)c;
- match++;
- }
- p1++;
- p2++;
- }
- return match;
- }
- int main(void)
- {
- static const char sample_word[] = "facebook";
- static char input_word[sizeof sample_word] = { 0 };
- int c;
- int tries = 0;
- int match = 0;
- int m;
- /* just fill in our input word with asterisk */
- for (c = 0; c < sizeof sample_word; c++) {
- input_word[c] = '*';
- }
- while (tries < 3) {
- printf("\nword is: %s\n", input_word);
- printf("enter a letter: ");
- c = getchar();
- while (getchar() != '\n');
- if (m = match_letters(sample_word, input_word, c)) {
- match += m;
- printf("match = %d, m = %d\n", match, m);
- if (match == strlen(sample_word)) {
- printf("done.\n");
- break;
- }
- continue;
- }
- tries++;
- printf("number of tries left: %d\n", tries);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement