C中的凯撒密码

时间:2014-02-25 04:51:25

标签: c encryption cs50 caesar-cipher

我已经盯着这个问题好几个星期了,我一无所有!它不起作用,我知道的很多,但我不知道为什么或什么是错的。我知道开发人员吐出了关于我突出显示的那条线的“错误:预期表达”,但实际上这只是冰山一角。如果有人知道如何修理任何一小部分,我会非常感激!!!!!

好的,我改变了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;
}

2 个答案:

答案 0 :(得分:0)

=&GT;不是有效的运营商。使用&gt; =

另外,我&lt;你的for循环中的n,而不是n&lt;岛

并且你的第二个for循环并不是必需的。只是把(明文);

答案 1 :(得分:0)

更改以下行

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

  

if(plaintext [i]&gt; ='A'&amp;&amp; plaintext [i]&gt; ='Z')

您应该使用&gt; = 而不是 =&gt;