CS50 凯撒密码

标签 c encryption cs50 caesar-cipher

我已经盯着这个问题好几个星期了,但我一无所获!它不起作用,我知道那么多,但我不知道为什么或出了什么问题。我确实知道开发人员针对我突出显示的行吐出了“错误:预期表达式”,但这实际上只是冰山一角。如果有人知道如何修复其中的任何一小部分,我将不胜感激!

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

int main(int argc, char *argv[]) 
{

    //Get the key
    if (argc != 2 || atoi(argv[1]) < 0)
    {
        printf("Usage: ./caesar k");
        return 1;
    }

    int key = atoi(argv[1]);
    string plaintex;
    string plaintext = GetString();

    for (int i = 0, n = strlen(plaintext); n < i; i++)
    {
        if (plaintext[i] > 'A' && plaintext[i] <= 'Z')
        {
            plaintext[i] = (plaintext[i] - 'A' + key) % 26 + 'A';
        }
    }  
    for (int i = 0, n = strlen(plaintext); n < i; i++)
    {           
        if (plaintext[i] >= 'A' && plaintext[i] >= 'Z')  // Highlighted line
        {
            plaintext[i] = (plaintext[i] - 'A' + key) % 26 + 'A';
        }
        else if (plaintext[i] >= 'a' && plaintext[i] < 'z')
        {
            plaintext[i] = (plaintext[i] - 'a' + key) % 26 + 'a';
        }
        else
        {
            printf("%c\n", plaintext[i]);
        }
    }
    return 0;
}

最佳答案

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

应该是

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

关于CS50 凯撒密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22007337/

相关文章:

c - 检测文本文件结尾,使用 fgetc 读取

android - 如何加密 Assets

c - 为什么 while 循环中的条件语句会导致程序选择性地永远暂停?

c - 为什么在下标中使用 CONSTANT 时会出现错误?

c - 以下 for 循环语法在 C 中是什么意思?

c++ - Mac OS X 声音输出

ios - SWIFT - Realm 数据库加密不起作用

encryption - 是否有 8 位 block 大小的公私 key 加密算法?

返回姓名首字母的 C 程序 (CS50)

c - 为什么我使用以下代码从 valgrind 获取 "invalid read"和 "invalid write"?