【发布时间】:2016-06-21 20:42:40
【问题描述】:
我对编码真的很陌生,我一直在自学如何使用 EDX.org 进行编码。这周我一直在学习密码学,我必须创建一个 Vigenère 密码。我编写了代码,在大多数情况下,它是正确的。但是,当我编译程序时,它显示了分段错误。我一直试图弄清楚为什么会发生这种情况,我完全被困住了。你能看看我的代码并告诉我有什么问题吗?
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int index(int k, int c);
int main (int argc, string argv[1])
{
//check for correct criteria
if (argc = 2, isalpha(argv[1]))
{
string text = GetString();
string key = argv[1]; //store key word
int Totalshift = strlen(key); //number of shift for keyword
int shift = 0;
//loops over the whole text
for (int i = 0, n = strlen(text); i <n; i++ )
{
char p= text[i];
char k = toupper(key[shift]); //Upper case for each character
if (isupper(p))
{
//convert to 0 index
p = p - 65;
k = k - 65;
int crypt= index (k , p);
printf("%c", crypt+65);
shift= (shift+1) %Totalshift;
}
else if (islower(p))
{
p = p - 97;
k = k - 65;
int crypt= index (k , p);
printf("%c", crypt+97);
shift= (shift+1) %Totalshift;
}
else
{
printf("%c", p);
}
}
printf("\n");
}
//error message
else
{
printf("ERROR!\n");
return 1;
}
}
//index function
int index(int k, int p)
{
return (k+p)% 26;
}
【问题讨论】:
标签: c compiler-errors segmentation-fault cs50 vigenere