【问题标题】:Vigenere cipher code in CC中的Vigenere密码
【发布时间】:2015-11-29 22:48:31
【问题描述】:
#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 语句带来了同样的错误。

【问题讨论】:

  • 第一个else没有对应的if。您的意思是在其中一个块中包含string key = argv[1];
  • 你不能有超出 if 范围的代码或 if-else 之间的 else

标签: c vigenere cs50


【解决方案1】:

我认为问题在于您不能将声明拆分为 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&lt;pp 是一个字符串。此外,由于您没有在循环中更新n,因此您应该在外部对其进行初始化

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

此外,尽管声明了 n 并将其指定为 strlen( p ),但您仍可以在 for 循环中使用函数调用。这没有什么问题,使用您分配的变量可能更有意义。

【讨论】:

    【解决方案2】:

    if 和 else if 语句之间不能有代码。

    string key = argv[1]; 不能在那里

    【讨论】:

      猜你喜欢
      • 2016-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多