C 谁能告诉我什么';我的凯撒算法有什么问题?

C 谁能告诉我什么';我的凯撒算法有什么问题?,c,cs50,caesar-cipher,C,Cs50,Caesar Cipher,当我使用数字4-22时,输出不稳定,我不知道为什么。非常感谢您的帮助,我也想知道为什么这样做行不通 #include<cs50.h> #include<stdio.h> #include<string.h> int main(void) { int shifts; int enc; printf("What is your message "); string message = get_string(); printf

当我使用数字4-22时,输出不稳定,我不知道为什么。非常感谢您的帮助,我也想知道为什么这样做行不通

#include<cs50.h>
#include<stdio.h>
#include<string.h>
int main(void)
{
    int shifts;
    int enc;
    printf("What is your message ");
    string message = get_string();
    printf("By how many letters do you want to shift?");
    scanf("%d",&shifts);

    for(int i=0;i<strlen(message);i++)
    {
           enc=((message[i] - 89)+shifts)%26;
           printf("%c",enc + 89);
    }
    printf("\n");
}
#包括
#包括
#包括
内部主(空)
{
整数移位;
int enc;
printf(“你的信息是什么”);
字符串消息=获取字符串();
printf(“您希望移动多少个字母?”);
scanf(“%d”、&shifts);

for(int i=0;i在for循环中,您应该检查字符是否为大写、小写或两者都不是。数字89也是错误的,即“Y”,您可能希望分别为65或97、“a”和“a”。for循环应该更改为类似于:

#include <ctype.h> // For isupper()/islower()
for(int i = 0; i < strlen(message); i++)
{
    if(isupper(message[i])) { // Check if current char is uppercase
        enc=((message[i] - 'A')+shifts)%26; // Apply cipher
        printf("%c",enc + 'A'); 
    } else if(islower(message[i])) { // Check if current char is lowercase
        enc=((message[i] - 'a')+shifts)%26; // Apply cipher
        printf("%c",enc + 'a');
    } else { // Print char without change
        printf("%c", message[i]);
    }
}
#包括//用于isupper()/islower()
对于(int i=0;i

请注意,使用“A”和“A”而不是65和97,它们将在编译时转换为相应的整数文本。还有其他编写方法,这可能不是最干净的方法(例如多个printf()),但它应该说明这是如何工作的,并且应该这样做。

数字
89
似乎是错误的。此外,您应该分别处理小写和大写。使用字符常量,而不是像
89
这样的幻数。此外,代码中存在内存泄漏。而且,这不是c@KeithNicholas实际上是C,他用的是那个讨厌的东西叫cs50。