我正在C中做作业,我正在制作一个刽子手游戏,从我们提供的文本文档中随机选择一行。
目前我有两个单词显示,但中间有一个逗号,我找不到正确的方法来改变它,以便它的空间。
我目前的代码在这里
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(int argc, char *argv[]) {
static char words[288][25];
FILE *file;
int i, j;
size_t count, n;
char word[25];
srand(time(NULL));
file = fopen("/home/dan/Downloads/hangman_text.txt", "r");
if (file == NULL){
printf("The file cannot be opened.\n");
return 1;
}
count = 0;
while(1==fscanf(file, "%24s", word)){
if(count == 288)
break;
strcpy(words[count++], word);
}
fclose(file);
n = count;
if(n > RAND_MAX){
int part;
size_t random = 0;
i = n / RAND_MAX;
part = rand() % (i+1);
if(part == i){
j = n % RAND_MAX;
if(j != 0)
random = random + RAND_MAX*part + rand() % j;
else
random = random + RAND_MAX*(part-1) + rand();
} else {
random = random + RAND_MAX*part + rand();
}
printf("%s\n", words[random]);
} else {
int random = rand() % n;
printf("%s\n", words[random]);
}
return 0;
}