我正在为使用已经输入的名称播放 Hangman 的类编写一个小的 C 代码。一个部分要求我允许输出带有 * 的输入短语来代替所有字母,但不允许使用标点符号。类似地,在短语的末尾,用户的名字放在括号中,意味着按原样打印。代码的第一部分工作正常,第一个 while 循环放置星号,但第二个 while 循环似乎每次都失败,并且似乎每次运行程序时都存储无意义和随机字符。这是我到目前为止的程序。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int guesses = 3;
int limit = 41;
char quote[42] = "I just wrote this game in C! (Josh Masher)";
char strArr[42];
char *quoP;
quoP = "e[0];
char *strP;
strP = &strArr[0];
while (*quoP != '.' && *quoP != '!' && *quoP != '?') {
if (isalpha(*quoP)) {
*strP = '*';
} else if (*quoP == ' ' || *quoP == ',') {
*strP = *quoP;
}
strP++;
quoP++;
}
while (*quoP != NULL) {
*strP = *quoP;
strP++;
quoP++;
}
}
有任何想法吗?
编辑
我稍微改写了代码,把随机字符的问题抹掉了,不过现在更复杂了。
int main()
{
int guesses = 3;
int limit = 41;
char quote[42] = "I just wrote this game in C! (Alex Butler)\0";
char strArr[42];
char *quoP;
quoP = "e[0];
char *strP;
strP = &strArr[0];
int counter = 0;
while (*quoP != '\0') {
if (*quoP == '.' || *quoP == '!' || *quoP == '?' || counter == 1) {
counter = 1;
}
if (isalpha(*quoP)) {
if (counter == 0) {
*strP = '*';
} else if (counter == 1) {
*strP = *quoP;
}
} else {
*strP = *quoP;
}
printf("%c", *strP);
strP++;
quoP++;
}
}