gpt4 book ai didi

检查字符串中的每个字符会产生意想不到的结果

转载 作者:太空宇宙 更新时间:2023-11-04 07:14:15 24 4
gpt4 key购买 nike

我正在制作一个 base64 编码器/解码器,我有一个函数可以将输入二进制编码为它的 base64 表示形式。输入是 01字符串

我的 if 语句无法验证 string 中的每个 char 是否为“0”或'1'。它表示每个 char 都不是“0”或“1”。尽管它是。

我的代码:(不是完整的功能代码...)

#include <stdio.h>
#include <string.h>

char *enc(char *);

int main(void)
{
enc("1101010100100101001010010101001");
return 0;
}

char *enc(const char *data)
{
int i;
for(i = 0; i < strlen(data); i++)
{
if(data[i] != '0' || data[i] != '1')
{
printf("index %d is not 0 or 1\n", i);
printf("instead it is: %c\n", data[i]);
}
}
return NULL;
}

这会输出每个 char NOT 01

IDEone:http://ideone.com/0MqNN4

最佳答案

if(data[i] != '0' || data[i] != '1') 

如果 char 不是 0 char 不是 1 -> 始终为真。我想你的意思是:

if(data[i] != '0' && data[i] != '1') 

英文中的“not a or b”在 bool 逻辑中翻译为“not (a or b)”,根据德摩根定律,相当于“(not a) and (not b)”。

关于检查字符串中的每个字符会产生意想不到的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26202076/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com