Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 接受不区分大小写的输入并正确打印_C_Arrays_String_Printf - Fatal编程技术网

C 接受不区分大小写的输入并正确打印

C 接受不区分大小写的输入并正确打印,c,arrays,string,printf,C,Arrays,String,Printf,嗨,伙计们,我写了一个更好和改进的刽子手游戏,它接受小写输入和大写输入。 我唯一的问题是,例如,如果字符串看起来像这个“tnt”,它会打印出控制台中的每个大字符。我试图让它打印出完全相同的字符串。 所以如果我输入大的'T',那么它应该接受它并输出小的'T' 有人能帮我吗 先谢谢你 #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> #incl

嗨,伙计们,我写了一个更好和改进的刽子手游戏,它接受小写输入和大写输入。 我唯一的问题是,例如,如果字符串看起来像这个“tnt”,它会打印出控制台中的每个大字符。我试图让它打印出完全相同的字符串。 所以如果我输入大的'T',那么它应该接受它并输出小的'T'

有人能帮我吗

先谢谢你

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

#define MAX_WORD 100

int get_word(char *word, int size) {
    fgets(word, size, stdin);
    int len = strlen(word);
    if (len && word[len - 1] == '\n')
        word[--len] = '\0';
    for (int i = 0; i < len; ++i)
        if (isalpha(word[i]))
            word[i] = toupper(word[i]);
        else
            return -1;
    return len;
}

void create_output(char *output, int len) {
    for (int i = 0; i < len; ++i)
        output[i] = '_';
    output[len] = '\0';
}

void print_output(const char *output, const char *alpha, int tries) {
    printf("(%d)  ", tries);
    for (int i = 0; output[i]; ++i)
        printf("%c ", output[i]);
    printf("  %s\n", alpha);
}

bool play_game(const char *word, int len) {
    char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char output[MAX_WORD];
    create_output(output, len);
    int tries = 0, guessed = 0;
    while (tries < len && guessed < len) {  // using word length for number of tries (?!)
        print_output(output, alpha, tries);
        printf("Guess: ");
        char guess;
        scanf(" %c", &guess);
        guess = toupper(guess);
        if (alpha[guess - 'A'] != ' ') {
            for (int i = 0; i < len; ++i)
                if (word[i] == guess && output[i] == '_') {
                    output[i] = word[i];
                    ++guessed;
                }
            alpha[guess - 'A'] = ' ';
            ++tries;
        }
    }
    print_output(output, alpha, tries);
    return guessed == len;
}

int main() {
    printf("Enter word to guess: ");
    char word[MAX_WORD];
    int len = get_word(word, sizeof word);
    if (len == -1) {
        printf("The word cannot contain spaces.\n");
        return EXIT_FAILURE;
    }
    if (play_game(word, len))
        printf("You win!\n");
    else
        printf("You lose.\nThe word was %s.\n", word);
    return 0;
} 
#包括
#包括
#包括
#包括
#包括
#定义最大单词100
int get_单词(字符*单词,int大小){
fgets(单词、大小、标准输入);
int len=strlen(字);
if(len&&word[len-1]=='\n')
字[--len]='\0';
对于(int i=0;i
有一个
tolower
函数,其工作方式与
toupper
几乎相同(有一个明显的区别)。除此之外:在调用
touper
之前,您不需要检查
isalpha
,这是安全的。这不是强制性的,您应该养成在for/if/…中使用{}的习惯。。。即使后面只有一句话。尤其是在'for(inti=0;i