I'm not complaining anything about the code results but, when I run the code, the IDE immediately stops working.
I tried many IDEs and editors and still doesn't do anything and there is no error or warning.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define NUM_STRING 4
#define MAX_STRING_SIZE 40
char* getWord()
{
static char words[NUM_STRING][MAX_STRING_SIZE] = {
"game",
"hello",
"program",
"pointer"
};
int randomIndex = rand() % 4;
char* selectedWord = words[randomIndex];
return selectedWord;
}
char GuessLetter()
{
char letter;
int value=1;
while(value)
{
printf("\nmake a guess: ");
if(scanf("%c", &letter) != 1)
{
printf("Please re-enter a letter");
}
else
{
value = 0;
}
}
return letter;
}
int checkchar(char* PW)
{
int i, len = strlen(PW);
for(i=0; i < len; i++)
{
if(PW[i]=='_')
{
return 1;
}
}
return 0;
}
char* printafter( char *currentWord, int len, char letter, char *PW)
{
int i;
for(i=0; i<len; i++)
{
if(letter == currentWord[i])
{
PW[i] = letter;
}
printf("%c ", PW[i]);
}
return PW;
}
void startGame()
{
char* currentWord = getWord();
char *PartialWord=NULL;
int i, length = strlen(currentWord);
printf("%s\n", currentWord);
for(i=0; i<length; i++)
{
PartialWord[i] = '_';
printf("_ ");
}
if(!(PartialWord = (char*)malloc(length)))
{
printf("Sorry there was an allocation error! ");
exit(1);
}
while(checkchar(PartialWord))
{
PartialWord = printafter(currentWord, length, GuessLetter(), PartialWord);
}
}
int main() {
srand(time(NULL));
startGame();
return 0;
}