我是编程新手。这是到目前为止我编写的代码。忽略加密本身的细节;我知道这将需要更多的工作。当我尝试运行该程序时,我收到一条分段错误错误消息。如果argc != 2
我会收到消息并且如果argc == 2
它打印出“关键字”但它显示相同的消息并且没有完成程序,那么我认为该错误与引用 argv[1] 有关。
#include<stdio.h>
#include<cs50.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
int main (int argc, string argv[])
{
int i = 0, n = strlen(argv[1]);
char KeyWord[i];
//makes sure command line has 2 arguements
if (2 != argc)
printf("argc != 2. Try again\n");
return 1;
//stores argv[1] as key
for (i = 0; i < n; i++)
{
KeyWord[i] = argv[1][i]; //malloc
printf("%c", KeyWord[i]);
}
printf("\n");
if (isalpha(KeyWord))
return 0;
else
{
printf("try again");
return 1;
}
int j, length;
printf("input data: ");
string message = GetString();
for (i = 0; i < n; i++)
{
for (j = 0, length = strlen(message); j < length; j++)
{
if (islower(message[j]))
message[j] = message[j] -97 + KeyWord[i];
if (isupper(message[j]))
message[j] = message[j] -65 + KeyWord[i];
}
if (i==n) i = 0;
}
}