gpt4 book ai didi

c - 凯撒密码的实现有什么问题?

转载 作者:行者123 更新时间:2023-11-30 18:31:37 25 4
gpt4 key购买 nike

我正在尝试实现凯撒密码,但没有得到预期的输出。代码有什么问题?

Key: 3
Input: Hello
Output I'm getting: KNUUX
Expected Output: KHOOR

代码:

#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, string argv[])
{
if(argc!=2)
{
return 1;
}
int k, i;
k = atoi(argv[1]);
printf("Enter the String to Encrypt: ");
string s=GetString();
for(i=0; i<strlen(s); i++)
{
if('A'>=s[i]<='Z')
{
s[i]=((s[i] - 'A' + k)%26) +'A';
}
else if('a'>=s[i]<='z')
{
s[i]=((s[i] - 'a' + k)%26) +'a';
}
}
printf("The Encrypted Text is %s\n",s);
}

最佳答案

if('A'>=s[i]<='Z')

肯定没有按照您的预期行事。

您可能想要:

if ( (s[i] >= 'A') && (s[i] <= 'Z') )

关于c - 凯撒密码的实现有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21744557/

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