简体   繁体   English

卡在vigenere.c

[英]stuck with the vigenere.c

I am stuck in this example. 我陷入了这个例子。 Whenever I put the keyword 'bacon' I get the wrong answer. 每当我输入关键字“培根”时,我都会得到错误的答案。 Any idea why this is happening? 知道为什么会这样吗? I can't find the bug and I have tried a lot. 我找不到错误,并且尝试了很多。 Can you please help me? 你能帮我么?

When I run this: ./vigenere bacon and input the text Meet me at the park at eleven am the answer should be Negh zf av huf pcfx bt gzrwep oz , but instead I get Negh ne og tjs qaty bt syfvgb bm 当我运行此命令时: ./vigenere bacon ,输入文字Meet me at the park at eleven am ,答案应该是Negh zf av huf pcfx bt gzrwep oz ,但我却得到了Negh ne og tjs qaty bt syfvgb bm

Update: I made some changes as suggested by the comments but still having the same problem. 更新:我根据评论的建议进行了一些更改,但仍然存在相同的问题。

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

int main(int argc, char *argv[])
{
    //check if it has only two values
    if (argc != 2)
    {
        printf("Usage: ./vigenere keyword\n");
        return 1;
    }

    // check that every letter of keyword is alphabetic
    char *keyword = argv[1];
    int keylen = strlen(keyword);
    for (int i = 0, n = keylen; i < n; i++)
    {
        if(!isalpha(keyword[i]))
        { 
            printf("alphabetic keyword only!!!\n");
            return 1; 
        }

       if(isalpha(keyword[i]))
       {
           // if a letter of keyword is uppercase
           if (isupper(keyword[i]))
           { 
               keyword[i] = keyword[i] - 'A';
           } 

           // if a letter of keyword is lowercase
           if (islower(keyword[i]))
           {
               keyword[i] = keyword[i] - 'a';   
           } 
       }  
    } 

    char *text = GetString();
    for (int i = 0, n = strlen(text); i < n; i++)
    {    

        if (isalpha(text[i]))
        {
            // if plaintext letter uppercase then...
            if (isupper(text[i]))
            {
                text[i] = ((text[i] - 'A') + (keyword[i % keylen])) % 26 + 'A';
                printf("%c",text[i]);

                                           // ^^^^^^ ----> repeat the pattern
            }

            // if plaintext letter lowercase then ...
            if (islower(text[i]))
            {
                text[i] = ((text[i] - 'a') + (keyword[i % keylen])) % 26 + 'a';
                printf("%c",text[i]);

            }
        }

        // if no letters in plaintext then ...
        if (!isalpha(text[i]))
        { 
            text[i] = text[i];
            printf("%c",text[i]);
        }  
    }
    printf("\n");
}

So as @BLUEPIXY suggested I had to use a different variable in order to get the right answer: 因此,如@BLUEPIXY所建议,我必须使用其他变量才能获得正确的答案:

   `char *text = GetString();
    int k = 0;
    for (int i = 0, n = strlen(text); i < n; i++)
    {    

    if (isalpha(text[i]))
    {
       // if plaintext letter uppercase
       if (isupper(text[i]))
       {
            text[i]= ((text[i] - 'A') + (keyword[k % keylen])) % 26 + 'A';
            printf("%c",text[i]);

                                        //^^^^^ ----> repeat the keyword pattern   
       }

            // if plaintext letter lowercase
       if (islower(text[i]))
        {
            text[i] = ((text[i] - 'a') + (keyword[k % keylen])) % 26 + 'a';
            printf("%c",text[i]);

        }
        k++;
     }`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM