Vigenere Cipher

时间:2016-08-02 18:07:05

标签: c vigenere cs50

我在运行下面为vigenere cipher.even设计的代码时遇到了问题。在彻底完成后我无法调试问题。它显示错误:由server.please帮助杀死。

 /**
 * 
 * vigenere.c
 * 
 * Abhishek kumar
 * encrypts entered string using vigenere cipher
 * */
 #include <stdio.h>
 #include <stdlib.h>
 #include <cs50.h>
 #include <ctype.h>
 #include <string.h>

 int main(int argc, string argv[] )
 {
    if (argc != 2)
    {
        printf("Usage: /home/cs50/pset2/vigenere <keyword>");
        return 1;
    }

    if (argc == 2)
    {   string key = argv[1];
        for(int k = 0,l = strlen(key);k < l; k++)
        {
            if(!isalpha(key[k]))
            {
                printf("Keyword must only contain letters A-Z and a-z");
                exit(1);
            } 

        }


        string txt = GetString();
        int i = 0,j = 0,c = 0;
        int n = strlen(txt);
        int m = strlen(key);
        while(i < n)
        {
            if (isupper(txt[i]))
            {
                if(isupper(key[j]))
                {
                    c = ((((int) txt[i] - 65 + (int) key[j] -65)%26) + 65);
                    printf("%c", (char) c);
                    i++;
                    j++;
                }
                if(islower(key[j]))
                {
                    c = ((((int) txt[i] - 65 + (int) key[j] -97)%26) + 65);
                    printf("%c", (char) c);
                    i++;
                    j++;
                }

            }
            else if (islower(txt[i]))
            {
                if(isupper(key[j]))
                {
                    c = ((((int) txt[i] - 97 + (int) key[j] -65)%26) + 97);
                    printf("%c", (char) c);
                    i++;
                }
                if(islower(key[j]))
                {
                    c = ((((int) txt[i] - 97 + (int) key[j] -97)%26) + 97);
                    printf("%c", (char) c);
                    j++;
                }


            }
            else
            {
                printf("%c",txt[i]);
                i++;

            }
            if (j == m-1)
            {
                j = 0;
            }
        }




    }
 }
下面是一些失败的测试用例。

:) vigenere.c exists
:) vigenere.c compiles
:( encrypts "a" as "a" using "a" as keyword
   \ killed by server
:( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword
   \ killed by server
:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword
   \ expected output, but not "CGSFpp"
:( encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword
   \ expected output, but not "CASFPO"
:) handles lack of argv[1]
:) handles argc > 2
:) rejects "Hax0r2" as keyword

1 个答案:

答案 0 :(得分:0)

islower(txt[i])部分,您无法在所有情况下增加ij。在你不增加i的地方,即密钥的第一个字符和文本都是小写的地方,你最终会得到一个无限循环。

isupper(txt[i])部分,您在i部分中增加jisupper(key[j]),然后输入islower(key[j])部分,因为您使用{{1}而不是if

对于上述两种情况,请将else if更改为if(islower(key[j])),并在每个内部else if(islower(key[j]))块之后移动j++printf。至于if,请将i更改为while并将for作为其中的一部分。

在检查您是否应该重置i时,您将被关闭1. jm-1的有效索引,因此您不希望重置了。在key时执行此操作。

此外,将ASCII代码替换为它们代表的实际字符,以便更清楚您正在做什么。也不需要演员阵容。

j == m