我写了一个非常简单的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;
}
答案 0 :(得分:0)
从ascii table看,az的小写字母从0x6_到0x7_,大写的字母从0x4_到0x5_,这意味着我们要做的就是关掉第二个半字节,以便将小写字母转换为大写字母。
(a)a-z如下0110 xxxx,0111 xxxx
(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。
答案 1 :(得分:0)
对于仅ASCII输入,这根本就不费脑子。...只需利用C语言中的char
类型实际上可用作int
并进行数学运算的事实即可。
#include <stdio.h>
#include <stdlib.h>
int main(){
char ch;
printf("letter : ");
ch = getc(stdin);
if (ch >= 'a' && ch <= 'z')
ch -=32;
else if (ch >= 'A' && ch <= 'Z')
ch += 32;
printf("output : %c\n", ch);
return 0;
}
下面是另一个版本。您可以将lower_case
和upper_case
替换为您想要的其他任何字母,然后将其繁荣起来。
#include <stdio.h>
#include <string.h>
char convert(char ch){
const char *lower_case = "abcdefghijklmnopqrstuvwxyz";
const char *upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *p;
if ((p = strchr(lower_case, ch)) != NULL)
return *(upper_case+(p-lower_case));
else if ((p = strchr(upper_case, ch)) != NULL)
return *(lower_case+(p-upper_case));
else
return ch;
}
int main(){
char ch;
printf("letter : ");
ch = getc(stdin);
printf("output : %c\n", convert(ch));
return 0;
}