c - C中的Vigenere密码

标签 c vigenere cs50

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

int main( int argc,  string argv[])
{

    if(argc!=2)
    {
        printf("One more string \n");
    }
    string key = argv[1];
    else if(!isalpha(key))
    {
        printf("Prompt only alphabet letters\n");
    }
    else 
    {
        string p = GetString();

        for ( i=0, j=0, n=strlen(p); i<p; i++, j++)
        { 
            if(j>=stlrlen(p))
            {
                j=0;
            }

            if(isupper(p[i]))
            {
                printf("%c", ((p[i] +key[j])%26)+'A');
            }

            if(islower(p[i]))
            {
                printf("%c", ((p[i] + key[j])%26)+'a');
            }

            if(!isupper(p[i]) && !islower(p[i]))
            {
                printf("%c", p[i]);
            }
            printf("\n");
            return 0;
        }
    }
}

我的代码有什么问题? CS50 Appliance 给我带来 1 个错误

c:15:1 expected expression.

它在我的代码中有意义吗? 有人能帮我吗? 当我只是删除 else if 代码块时,它给我带来了同样的错误到 else 语句中。

最佳答案

我认为问题在于您不能将语句拆分为 ifelse ififelse

if(argc!=2)
{
    printf("One more string \n");
}
string key = argv[1]; // This is the problem ➞ it cannot sit in the middle of if and else or else if
else if(!isalpha(key))
{
    printf("Prompt only alphabet letters\n");
}

此外,您可能打算制作 else ifelse常规if声明,因为看起来你正在做一些验证

另外,因为你在你的 for 循环中你有 i<p但是p是一个字符串。此外,由于您不更新 n在循环中你应该在外面初始化它

int n = strlen( p );
for (int i=0, j=0; i<n; i++, j++) 

同样,尽管声明了 n 并将其赋值 strlen( p ) ,您在 for 循环中使用函数调用。这没什么不对,只是使用您分配的变量可能更有意义。

关于c - C中的Vigenere密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33989088/

相关文章:

c - 与 C 中指针的区别

c - stat结构错误(fstat系统调用)

c - Getopt - 需要输入

CS50 PSet 2 : Vigenere cipher Segmentation Fault

c - 为什么printf会打印我分配给int空间的空间数?

c - 如何在c中将库添加到 'make'命令

c - 哪里可以找到 'The Art and Science of C'源代码?

java - Java 中的 Vigenere/多字母密码解码器/解密器/解密器

Python 3 : Generate not all permutations, 但长度 r 的所有非重复组合?

c - 我的简短 C 代码中的小错误。为什么?