【发布时间】:2019-01-30 02:46:57
【问题描述】:
1.
如果 3 字符密码包含数字,则将 hasDigit 设置为 true。
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
int main(void) {
bool hasDigit;
char passCode[50];
hasDigit = false;
strcpy(passCode, "abc");
/* Your solution goes here */
if (hasDigit) {
printf("Has a digit.\n");
}
else {
printf("Has no digit.\n");
}
return 0;
}
我尝试过的(代替 /* 你的解决方案放在这里 */ 是:
if (isdigit(passCode) == true) {
hasDigit = true;
}
else {
hasDigit = false;
}
测试时
abc
它可以工作,但是在测试时
a 5
它不起作用。
2.
将 2 字符字符串 passCode 中的任何空格 ' ' 替换为 '_'。给定程序的示例输出:
1_
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
char passCode[3];
strcpy(passCode, "1 ");
/* Your solution goes here */
printf("%s\n", passCode);
return 0;
}
我代替 /* 你的解决方案在这里 */ 是:
if (isspace(passCode) == true) {
passCode = '_';
}
而且编译失败。
感谢大家的帮助。
【问题讨论】:
-
isdigit(...)接受一个字符,但您传递的是一个数组。尝试遍历每个字符并传递passCode[i] -
isdigit(passCode[i]) 导致编译器告诉我: 在 main.c:4:0: main.c 包含的文件中:在函数'main'中:main.c:13: 25: error: 'i' undeclared (first use in this function) if (isdigit(passCode[i]) == true) { ^ main.c:13:25: 注意:每个函数只报告一次未声明的标识符它出现在
-
也不需要
== true,如果是真的,if会检测到 -
@SOMEK 你需要声明
int i;并初始化它,创建一个for(int i=0; i<3; i++)循环来检查数组中的每个字符 -
查看文本或教程中的循环控制。它是基本的 C 语言(以及几乎任何其他语言),您将在余下的工程职业生涯中只使用它。错误很明显:你没有声明你的循环控制变量
i(如果你甚至编写了一个循环;我们不知道)。
标签: c