#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 语句中。
最佳答案
我认为问题在于您不能将语句拆分为 if
和 else if
或 if
和 else
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 if
和 else
常规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/