gpt4 book ai didi

当变量范围改变时,C 给出奇怪的输出

转载 作者:行者123 更新时间:2023-11-30 14:46:02 36 4
gpt4 key购买 nike

当我将 nonAlphaCount 声明放在 for 循环上方时,C 会执行一些令人毛骨悚然的事情。我无法解释为什么输出不同。

对于版本 1(主方法上方的 int 声明),我的输入输出是:输入:./Vigenere.exe 培根输入纯文本:上午十一点在公园见我输出:Negh zf av huf pcfx bt gzrwep oz

对于版本 2(上面 for 循环的 int 声明)输入:./Vigenere.exe 培根输入纯文本:上午十一点在公园见我输出:NRQQ M[L\M^^ KQXXZQZ M

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

const int INPUT_LEN = 255;
const int ALPHABET_LEN = 26;
int nonAlphaCount = 0;

int main (int count, char *args[])
{

char plainText[INPUT_LEN];
char *cipherText;
char *keyWord;

if ( count < 2 || count > 2)
{
printf("There is no key");
return 1;
}

strcpy(keyWord, args[1]);
int keyWord_LEN = strlen(keyWord);

printf("Enter plain text: ");
fgets (plainText, INPUT_LEN, stdin);

int strLength = strlen(plainText);
cipherText = malloc(strLength);

printf("%s", plainText);

for (int i = 0; i < strLength; i++ ){

if(plainText[i] == '\0' || plainText[i] == '\n'|| plainText[i] == '\r')
break;

if(isalpha(plainText[i]))
{
// Default lower
int asciiUpperOrLower = 97;
int keyUpperOrLower = 97;

if(isupper(plainText[i]))
asciiUpperOrLower = 65;

if(isupper(keyWord[i % keyWord_LEN]))
keyUpperOrLower = 65;

int Key = keyWord[(i - nonAlphaCount) % keyWord_LEN] - keyUpperOrLower;
int alphabetBaseletter = ((plainText[i] - asciiUpperOrLower + Key) % ALPHABET_LEN);

cipherText[i] = alphabetBaseletter + asciiUpperOrLower;
}
else{
cipherText[i] = plainText[i];
nonAlphaCount++;
}

}

// Set string terminator.
cipherText[strLength - 1] = '\0' ;

printf("%s", cipherText);

return 0;

}





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

const int INPUT_LEN = 255;
const int ALPHABET_LEN = 26;

int main (int count, char *args[])
{

char plainText[INPUT_LEN];
char *cipherText;
char *keyWord;

if ( count < 2 || count > 2)
{
printf("There is no key");
return 1;
}

strcpy(keyWord, args[1]);
int keyWord_LEN = strlen(keyWord);

printf("Enter plain text: ");
fgets (plainText, INPUT_LEN, stdin);

int strLength = strlen(plainText);
cipherText = malloc(strLength);

printf("%s", plainText);

**int nonAlphaCount = 0;**
for (int i = 0; i < strLength; i++ ){

if(plainText[i] == '\0' || plainText[i] == '\n'|| plainText[i] == '\r')
break;

if(isalpha(plainText[i]))
{
// Default lower
int asciiUpperOrLower = 97;
int keyUpperOrLower = 97;

if(isupper(plainText[i]))
asciiUpperOrLower = 65;

if(isupper(keyWord[i % keyWord_LEN]))
keyUpperOrLower = 65;

int Key = keyWord[(i - nonAlphaCount) % keyWord_LEN] - keyUpperOrLower;
int alphabetBaseletter = ((plainText[i] - asciiUpperOrLower + Key) % ALPHABET_LEN);

cipherText[i] = alphabetBaseletter + asciiUpperOrLower;
}
else{
cipherText[i] = plainText[i];
nonAlphaCount++;
}

}

// Set string terminator.
cipherText[strLength - 1] = '\0' ;

printf("%s", cipherText);

return 0;

}

最佳答案

两个程序都表现出未定义的行为

char *keyWord;
...
strcpy(keyWord, args[1]);

编译器警告显示:“使用了未初始化的局部变量‘keyWord’”。您尚未分配任何内存。

如果其中一个程序碰巧能运行,那就这样吧。

关于当变量范围改变时,C 给出奇怪的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52571467/

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