-1
我是编程新手。这是迄今为止我写的代码。忽视加密本身的细节;我知道这需要更多的工作。当我尝试运行该程序时,出现分段错误错误消息。如果argc != 2
我会得到消息,如果argc == 2
它打印出“关键字”,但它显示相同的消息,并没有完成程序,所以我认为这个错误与引用argv [1]有关。我的Vigenere加密程序中的分段错误
#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;
}
}
'字符串的argv []'这是什么? –
''提供'typedef char * string;' - 这就是'string argv []'的意思。 –