Sure:
Example
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void) {
char text[40]="EVEN BETTER HARRY POTTER", *mask;
char ch_in;
int kk, len, found, done;
/* Phrase not of my origin */
printf("Let's play Wheel-of-Fortune\n");
len = (int)strlen(text);
mask = (char *)malloc(len);
memset(mask, 0, len);
for(kk = 0; kk < len; kk++)
if(text[kk] != ' ')
printf("_ ");
else {
mask[kk] = 1;
printf(" ");
}
putc('\n', stdout);
done = 0;
do {
found = 0;
done = 1;
printf("Enter a letter: ");
ch_in = toupper(getchar());
// this is a nasty way of handling the newline that you get from
// getchar()
if(ch_in == '\n') {
putc('\r', stdout);
done = 0;
continue;
}
if(isalpha(ch_in)) {
for(kk=0; kk<len; kk++){
if((toupper(text[kk]) == ch_in) && (text[kk] != ' ')){
found = 1;
mask[kk] = 1;
putc(ch_in, stdout);
} else {
if(mask[kk])
putc(text[kk], stdout);
else {
if(text[kk] != ' ')
printf("_ ");
else
printf(" ");
done = 0; // if a letter is missing then we aren't done yet
}
}
}
putc('\n', stdout);
if(!found)
puts("No Match!");
} else
puts("Invalid character!");
printf("%d %d\n", found, done);
} while(!done);
puts("You win!");
free(mask);
return 0;
}