出于某種原因,我的代碼在 C 中不起作用。
請檢查以下代碼:
#include <stdio.h>
#include <string.h>
int main(){
while (1){
printf(“Is Python a programming language? “);
char question[4];
scanf(“%s”, &question);
if (strcmp(question, “yes”) == 0 || (strcmp(question, “YES”) == 0 || (strcmp(question, “Yes”) == 0 ))){ //the reason why I’m using so many “or” statements is because idk how to lowercase the input (pls help me if you can)
printf(“Correct”);
} else {
printf(“Incorrect”);
continue;
}
}
uj5u.com熱心網友回復:
- 您的
OR輸入 if 未涵蓋所有可能的排列。它也可以是yEsodyeS等。
char *strtouppper(char *str)
{
char *wrk = str;
while(*wrk) {*wrk = toupper((unsigned char)*wrk); wrk ;}
return str;
}
int main(void)
{
char answer[4];
while (1)
{
printf("Is Python a programming language? ");
if(scanf(" %3s", answer) != 1) {printf("\n\nInput error");break;};
if(!strcmp("YES", strtouppper(answer)))
printf("\nCorrect\n");
else
printf("\nIncorrect\n");
}
}
https://godbolt.org/z/vahd9be9M
uj5u.com熱心網友回復:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char * low(char * str){//a function for solve lowercase and uppercase and mix
for(int i = 0; str[i]; i ){
str[i] = tolower(str[i]);
}
}
int main(){
while (1){
printf("Is Python a programming language? ");
printf("\n");
char question[4];
scanf("%s", question);
low(question);
if (strcmp(question, "yes") == 0 ){
printf("\nCorrect");
break;}
else {
printf("\nIncorrect");
continue;}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/359740.html
上一篇:Python逆向工程——為什么代碼產生了這個特殊的結果
下一篇:如果函式-替換為公式
