簡體   English   中英

凱撒密碼

[英]Caesar Cipher In C

我已經盯着這個問題好幾個星期了,但我一無所有! 它不起作用,我知道很多,但我不知道為什么或出了什么問題。 我確實知道開發人員會針對我強調的那行吐出“錯誤:期望的表達”,但實際上那只是冰山一角。 如果任何人都知道如何解決這個問題,我將不勝感激!

好的,所以我像您建議的那樣更改了i <n和> =驚人的助手,它會一直運行並創建,但是仍然存在會導致這些難看的錯誤的故障:

:( encrypts "a" as "b" using 1 as key
   \ expected output, not an exit code of 0
:( encrypts "barfoo" as "yxocll" using 23 as key
   \ expected output, not an exit code of 0
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
   \ expected output, but not "E\nA\nU\nI\nR\nR\n"
:( encrypts "BaRFoo" as "FeVJss" using 4 as key

有什么建議么?

#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 plaintext = GetString();

    for (int i = 0, n = strlen(plaintext); i < n; i++)
    {
        if (plaintext[i] > 'A' && plaintext[i] <= 'Z')
            {
            plaintext[i] = (plaintext[i] - 'A' + key) % 26 + 'A';
            }
    }


    for (int i = 0, n = strlen(plaintext); i < n; i++)
    {           
       if (plaintext[i]  >= 'A' && plaintext[i] >= 'Z')
       {
       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;
}

=>不是有效的運算符。 使用> =

同樣,我<for循環中的<n,而不是n <i。

並且您的第二個for循環並不是真正必要的。 只是放(明文);

更改您的下一行

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

如果(純文本[i]> ='A'&&純文本[i]> ='Z')

您應該使用> =而不是=>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM