我编写了一个非常简单的Hangman游戏,该游戏要求用户输入,然后使用户猜测最多六次。该单词的第一个字母始终显示,并且如果用户输入的字母相同,那么就不会将其视为错误,如果want子手串由大写字母组成,我想改进代码以使其接受小写输入。例如,如果输入字符串为“ HELLO”,并且用户在控制台上看到此H_____并且输入小写字母“ e”,则该字母应被发现并打印为大写字母作为输入字符串。因此,在用户输入e之后,输出应如下所示:HE___但是,通过我实现的方式,它只是将所有大写字母转换为小写字母。
如果字符串中包含大写字母,如何使代码接受小写用户输入并打印出大写用户输入?
感谢您的帮助
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
char hangmanWord[100], tempWord[100];
char hangmanOutput[100];
int wrongTry = 6 , matchFound = 0;
int tries = 0;
int counter = 0 , position = 0, winner, length , i;
char alphabetFromUser;
scanf("%s",hangmanWord);
getchar();
length = strlen(hangmanWord);
for( i = 1; i < length ; i++)
{
hangmanOutput[i] = '_';
hangmanOutput[length] = '\0';
}
for(i = 0 ; i < length ; i++)
{
if(i==0){
printf("%c",hangmanWord[0]);
}
printf("%c",hangmanOutput[i]); //line output
if(i==length-1){
printf("(%d)",tries);
}
}
while(wrongTry != 0) /**while loop for exiting the program when no try left**/
{
matchFound = 0;
putchar('\n');
printf("letter: ");
scanf("%c",&alphabetFromUser);
fflush(stdin);
if (matchFound != 2)
{
for(counter=0;counter<length;counter++) /**for loop to check whether player input alphabet exists or not in the word**/
{
if(hangmanWord[counter]>= 'A' && hangmanWord[counter] <= 'Z'){
hangmanWord[counter] = tolower(hangmanWord[counter]);
}
if(alphabetFromUser==hangmanWord[counter])
{
matchFound = 1;
}//end of if()
}//end of
if(matchFound == 0) /**in case of wrong guess**/
{
wrongTry--;
tries++;
}//en
else
{
for(counter = 0; counter < length; counter++)
{
matchFound = 0;
if(alphabetFromUser == hangmanWord[counter])
{
position = counter ;
matchFound = 1;
}//end of if
if(matchFound == 1)
{
for(i = 0 ; i < length ; i++)
{
if( i == position)
{
hangmanOutput[i] = alphabetFromUser; /**Put the alphabet at right position**/
}
else if( hangmanOutput[i] >= 'a' && hangmanOutput[i] <= 'z' ) /** If the position already occupied
by same alphabet then no need to
fill again EASY!! and continue */
{
continue;
}
else
{
hangmanOutput[i] = '_'; /** Put a underscore at not guessed alphabet position **/
}
}
tempWord[position] = alphabetFromUser; /**put the alphabet in another char array to check with the original word**/
tempWord[length] = '\0';
/**put the NULL character at the end of the temp string**/
winner = strcmp(tempWord+1,hangmanWord+1); /**upon True comparison it will return 0**/
if(winner == 0) /**if the player guessed the whole word right then he/she is the WINNER**/
{
for(i = 0 ; i < length ; i++)
{
if(i==0){
printf("%c",hangmanWord[0]);
}
printf("%c",hangmanOutput[i+1]); //line output
if(i==length-1){
printf("(%d)",tries);
}
}
getchar();
return 0;
}//end of inner if
}//end of outer if
}//end of for loop
}//end of else
} // end of if(matchFound != 2) condition
for(i = 0 ; i < length ; i++)
{
if(i==0){
printf("%c",hangmanWord[0]);
}
printf("%c",hangmanOutput[i+1]); //line output
if(i==length-1){
printf("(%d)",tries);
}
}
getchar();
}
if(wrongTry <= 0) /**if the player can not guess the whole word in 5 chaces**/
{
putchar('\n');
printf("DEAD\n");
return 0;
}
getchar();
return 0;
}
((b)A-Z如下0100 xxxx,0101 xxxx
让我们假设字符'a'为0110 xxxx。如果我们将AND按位与0x5F => 0110 xxxx&0101 xxxx => 0100 xxxx =>'A'
char upper(char ch) {
return(ch & 0x5F);
}
现在我们有了功能,我们可以使用它。查看您的代码,您将检查hangman数组两次,以防用户键入小写字母或大写字母,所以我们可以这样做一次,而不是这样做。
int32_t right_letter; int32_t tries; int32_t win; right_letter = 0; /* since one letter is already discovered */ win = length - 1; while(wrongTry != 0) { /* read from user */ for(counter = 0; counter < length; ++counter) { if(hangmanOutput[counter] == '_'){ if(upper(alphabetFromUser) == upper(hangmanWord[counter])) { hangmanOutput[counter] = hangmanWord[counter]; right_letter = 1; ++win; } } if(win + 1 == length) /* win */ wrongTry -= !right_letter; right_letter = 0; }
我还没有测试代码,但是应该可以。尝试了解我已更改的内容以及原因。
编辑:@Andrew Henle在评论中提到OP没有指定字符集,因此我认为它是ASCII。