C:Vigenere密码编译但是分段错误 - 指针?

时间:2017-03-16 19:14:37

标签: c segmentation-fault vigenere

我在此代码中的某个地方遇到了分段错误,但它已成功编译。代码是通过密钥重复迭代以加密明文字。我假设它与指针相关,但我不知道如何解决它。

另外:这是表达isalpha否定的合适方式吗?它没有抛出错误。当我尝试在它之后使用==FALSE时,它确实会抛出错误。

int main(int argc, char* argv[]) 
{

    if ((argc<2) || (argc>2) || (!isalpha(argv)))//exits if non-alpha key or < or > than 2 command line inputs 
    {
        printf("Please enter your single word key"); //prompts for appropriate key
        return 1; 
    }

    if(argc==2) // requires two command line inputs with non-integer key to continue
    {
        printf("plaintext:");      //Printed on terminal before word to be enciphered 
        char* word = GetString();  //Prompts user to enter word required to be enciphered

        printf("ciphertext:");   

    char* key = (argv[1]);  //key = initial user command line entry   


    int i = 0;  
    int k = 0;     //defines characters in key   

    for(i=0; i<strlen(word); i++)    //iterates through word entered by user as plaintext
    {   

        if(isalpha(key[k]))
        {

            if(isupper(word[i]))    //if original characters are uppercase 
            {
                int cipher = (word[i] + key[k] -65) % 26 + 65; 
                printf("%c", cipher);

            }
            else if(islower(word[i]))  //if original characters are lowercase
            {
                int cipher = (word[i] + key[k] - 97) % 26 + 97; 
                printf("%c", cipher);
            }        
            else    //for all other types of characters
            {
                printf("%c", word[i]); 
            } 

        }    
        else if(!isalpha(key[k]))
        {
            return 1;
        }


        if (word[i]==strlen(word)) //if reaches end of plaintext word
        {
            printf("\n");   //print a new line and exit
            return 1;      
        }    

        else if(word[i]<strlen(word))  //if haven't reached end of word, increment k 
        {
            k++;
        }    

        if (key[k]==strlen(key))
        {
            k = 0;
        }    

    }
    printf("\n"); 

    }

}    

0 个答案:

没有答案