gpt4 book ai didi

在游戏中将输入转换为大写

转载 作者:行者123 更新时间:2023-11-30 16:08:30 24 4
gpt4 key购买 nike

我编写了一个非常简单的 Hangman 游戏,它请求用户输入,然后让用户猜测最多六次。始终显示单词的第一个字母,如果用户输入已经被发现的相同字母,则不会被视为错误,我想改进我的代码,使其在 Hangman 字符串由大写字母组成时接受小写输入。例如,如果输入字符串是“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;
}

最佳答案

查看ascii table ,从a到z的小写字母是从0x6_到0x7_,大写字母是从0x4_到0x5_,这意味着我们所要做的就是关闭第二个半字节的第二位,以便将小写字母转换为一个大写字母。

(a) a-z 如下:0110 xxxx, 0111 xxxx

(b) A-Z 如下:0100 xxxx, 0101 xxxx

假设我们有字符“a”,即 0110 xxxx。如果我们按位与 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。

关于在游戏中将输入转换为大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59335717/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com