我想用 C 写一个代码,其中声明了密码的字符数,但我不知道为什么在使用以下算法后,我得到了奇怪的答案。
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
int n=1,i=0;
char * pass[100];
printf("Enter your pass: ");
scanf(" %c",&pass);
for (i=0; pass[i]!='0';i++){
n++;
}
printf("number of characters: %d\n", n);
return 0;
}
最佳答案
从 char * pass[100]
开始可能不是您想要的,尽管它可能有效。您只需要 char pass[100]
。
其次,您要求 scanf
使用 %c
选项扫描单个字符。要读取整个字符串,请使用 %s
选项。
最后一行:
对于 (i=0; pass[i]!='0';i++){
'0'
并不表示空,它指的是字符 0,即 ascii 图表上的 48。您可能正在寻找 '\0'
,这意味着 null。 Null 用于终止字符串。
关于c - C中密码中的字符数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40712850/