我不知道为什么我';m接收分段错误 #包括 #include//stdlib.h包含在cs50.h中,所以我不需要它 #包括 #包括 #包括 int main(int argc,字符串argv[])//命令行输入 { if(argc!=2)//检查是否只有一个输入 { printf(“错误\n”); 返回1; } int commandlength=strlen(argv[1]);//查找命令字符串的字符串长度 字符串键[commandlength+1];//从输入中获取键,并将其放入稍后需要较少键入的内容中 for(int i=0;i

我不知道为什么我';m接收分段错误 #包括 #include//stdlib.h包含在cs50.h中,所以我不需要它 #包括 #包括 #包括 int main(int argc,字符串argv[])//命令行输入 { if(argc!=2)//检查是否只有一个输入 { printf(“错误\n”); 返回1; } int commandlength=strlen(argv[1]);//查找命令字符串的字符串长度 字符串键[commandlength+1];//从输入中获取键,并将其放入稍后需要较少键入的内容中 for(int i=0;i,c,segmentation-fault,cs50,vigenere,C,Segmentation Fault,Cs50,Vigenere,在阅读之前,请记住我是一名学生。收到详细的解释比简单地说一行代码“给你,我修好了”更有益。我将在我的投稿中链接这篇文章,我们对学术诚信以及什么是复制还是帮助有非常严格的规定,如果记住这一点,我将非常感激 这个项目的目的是创建一个vigenere密码。以下是我的老师提供的说明: 本周的最后一个挑战是用vigenere.c编写一个程序,用Vigenère的密码对消息进行加密。这个程序必须接受一个命令行参数:关键字k,完全由字母字符组成。如果您的程序在执行时没有任何命令行参数、有多个命令行参数,或者有

在阅读之前,请记住我是一名学生。收到详细的解释比简单地说一行代码“给你,我修好了”更有益。我将在我的投稿中链接这篇文章,我们对学术诚信以及什么是复制还是帮助有非常严格的规定,如果记住这一点,我将非常感激

这个项目的目的是创建一个vigenere密码。以下是我的老师提供的说明:

本周的最后一个挑战是用vigenere.c编写一个程序,用Vigenère的密码对消息进行加密。这个程序必须接受一个命令行参数:关键字k,完全由字母字符组成。如果您的程序在执行时没有任何命令行参数、有多个命令行参数,或者有一个命令行参数包含任何非字母字符,则您的程序应立即投诉并退出,main返回1(从而表示我们自己的测试可以检测到的错误)。否则,您的程序必须继续提示用户输入一个纯文本字符串p,然后必须根据Vigenère的密码k对其进行加密,最终打印结果并退出,main返回0

对于k中的字符,必须将A和A视为0,B和B视为1,…,Z和Z视为25。此外,如果p中的字符是字母,则程序必须仅对该字符应用Vigenère密码。所有其他字符(数字、符号、空格、标点符号等)的输出必须保持不变。此外,如果您的代码要将k的第j个字符应用于p的第i个字符,但后者被证明是非字母字符,则必须等待将k的第j个字符应用于p的下一个字母字符;您还不能前进到k中的下一个角色。最后,程序必须在p中保留每个字母的大小写

我非常确定我达到了迭代代码的要求,确保密钥循环(如果它比要加密的短语短),并确保我没有将密钥应用于非字母字符。但是,在提示输入要加密的字符串之前,我得到了一个默认的分段,所以我很确定问题出在代码的上半部分。我已经仔细查看了代码,但仍然不明白它为什么会继续出现分段错误。

更改此语句

#include <stdio.h>
#include <cs50.h> //stdlib.h is included in cs50.h so I don't need it
#include <string.h>
#include <ctype.h>
#include <math.h>

int main(int argc, string argv[]) // command line input
{
    if(argc != 2) // check if there is only one input
    {
        printf("error\n");
        return 1;
    }

    int commandlength = strlen(argv[1]); // find string length of command string

    string key[commandlength + 1]; // taking the key from the input and putting it in something that will take less typing later

    for(int i = 0; i < commandlength; i++) // check if every char in the string is a letter
    {
        if(isalpha(argv[1][i]))
            continue;

        else
        {
            printf("error\n");
            return 1;
        }
    }

    strcpy(key[commandlength], argv[1]); // copy key from command line into a string called key
    string input = GetString();
    int inputlength = strlen(input); // length of string typed in when prompted
    int k = 0; // this will be used to iterate the key separately from i, since the key only iterates when applied to an alpha

    for(int i = 0; i < inputlength; i++)
    {
        if(ispunct(input[i]))
            printf("%c", input[i]);

        if(isspace(input[i]))
            printf("%c", input[i]);

        if(isupper(input[i]))
        {
            printf("%c", (input[i] + atoi(key[k]) % commandlength - 65) % 26 + 65);
            k++;
        }

        if(islower(input[i]))
        {
            printf("%c", (input[i] + atoi(key[k]) % commandlength - 97) % 26 + 97);
            k++;
        }        
    }

    printf("\n");
    return 0;
}
typedef char string;
int inputlength = strlen(input); 

键[commandlength]是char类型的对象,如果使用此无效构造,则其值用作字符串的地址

strcpy( key, argv[1] ); 
此外,在循环中使用
continue
也不是一个好主意。而不是

strcpy(key[commandlength], argv[1]); 
应该有

string key[commandlength + 1]; 
前提是您没有类似的typedef

char key[commandlength + 1]; 
但是,如果您确实有这样一个typedef,那么下面的语句

#include <stdio.h>
#include <cs50.h> //stdlib.h is included in cs50.h so I don't need it
#include <string.h>
#include <ctype.h>
#include <math.h>

int main(int argc, string argv[]) // command line input
{
    if(argc != 2) // check if there is only one input
    {
        printf("error\n");
        return 1;
    }

    int commandlength = strlen(argv[1]); // find string length of command string

    string key[commandlength + 1]; // taking the key from the input and putting it in something that will take less typing later

    for(int i = 0; i < commandlength; i++) // check if every char in the string is a letter
    {
        if(isalpha(argv[1][i]))
            continue;

        else
        {
            printf("error\n");
            return 1;
        }
    }

    strcpy(key[commandlength], argv[1]); // copy key from command line into a string called key
    string input = GetString();
    int inputlength = strlen(input); // length of string typed in when prompted
    int k = 0; // this will be used to iterate the key separately from i, since the key only iterates when applied to an alpha

    for(int i = 0; i < inputlength; i++)
    {
        if(ispunct(input[i]))
            printf("%c", input[i]);

        if(isspace(input[i]))
            printf("%c", input[i]);

        if(isupper(input[i]))
        {
            printf("%c", (input[i] + atoi(key[k]) % commandlength - 65) % 26 + 65);
            k++;
        }

        if(islower(input[i]))
        {
            printf("%c", (input[i] + atoi(key[k]) % commandlength - 97) % 26 + 97);
            k++;
        }        
    }

    printf("\n");
    return 0;
}
typedef char string;
int inputlength = strlen(input); 
将无效,因为您正在尝试调用函数strlen进行输入:

string input = GetString();
并且不要在代码中使用幻数,例如在本语句中

#include <stdio.h>
#include <cs50.h> //stdlib.h is included in cs50.h so I don't need it
#include <string.h>
#include <ctype.h>
#include <math.h>

int main(int argc, string argv[]) // command line input
{
    if(argc != 2) // check if there is only one input
    {
        printf("error\n");
        return 1;
    }

    int commandlength = strlen(argv[1]); // find string length of command string

    string key[commandlength + 1]; // taking the key from the input and putting it in something that will take less typing later

    for(int i = 0; i < commandlength; i++) // check if every char in the string is a letter
    {
        if(isalpha(argv[1][i]))
            continue;

        else
        {
            printf("error\n");
            return 1;
        }
    }

    strcpy(key[commandlength], argv[1]); // copy key from command line into a string called key
    string input = GetString();
    int inputlength = strlen(input); // length of string typed in when prompted
    int k = 0; // this will be used to iterate the key separately from i, since the key only iterates when applied to an alpha

    for(int i = 0; i < inputlength; i++)
    {
        if(ispunct(input[i]))
            printf("%c", input[i]);

        if(isspace(input[i]))
            printf("%c", input[i]);

        if(isupper(input[i]))
        {
            printf("%c", (input[i] + atoi(key[k]) % commandlength - 65) % 26 + 65);
            k++;
        }

        if(islower(input[i]))
        {
            printf("%c", (input[i] + atoi(key[k]) % commandlength - 97) % 26 + 97);
            k++;
        }        
    }

    printf("\n");
    return 0;
}
typedef char string;
int inputlength = strlen(input); 

我想65是A。因此,最好使用显式字符文字“A”而不是65。

我不确定
字符串的类型应该是什么(根据
main(int argc string argv[]
)行,它看起来像一个
char*
。如果是这样,那么您将得到一个带有该行的char指针数组
字符串键[commandlength+1];

strcpy(键[commandlength],argv[1]);

您将字符串从
argv[i]
复制到您创建的数组中的最后一个指针。但由于该指针(尚未)分配了任何内存。此外,它具有随机内容并指向随机内存位置。因此,您将获得SEGFULT


可能您想要的是一个指针,它将
argv[1]
字符串的
stringlength
作为内存关联。

用户johanneshau给出的答案有些正确。 要添加到他/她的答案中,您应该声明一个变量键,如下所示

printf("%c", (input[i] + atoi(key[k]) % commandlength - 65) % 26 + 65);
而不是

string key;
然后应该为上面的指针变量分配内存,因为string是char*的typedef,因此在分配内存之前,它不能存储任何字符串

string key[commandlength + 1];
上行将分配内存来保存输入字符串

然后应该使用fo将输入字符串复制到其中