Skip to main content
2 of 2
added 414 characters in body
Cliff B
  • 69.3k
  • 4
  • 33
  • 56

Why do i get these errors? i am new to programming so please be easy on me. The errors are in the body

#include<stdio.h>
#include<cs50.h>
#include<string.h>
#include<ctype.h>
bool check_validity_key(string s);


int main(int argc, string argv[])
{
    if (!check_validity_key(argv[1]))
    {
        printf("Key must contain only alphabetic characters\n");
    }
    
    int len = strlen(argv[1]);
    string key = argv[1];
    
    string plaintext = get_string (" Plaintext: ");
    printf("ciphertext: ");
    

    
    
    
    if(argc == 2)
    {
        if (len == 26)
        {
            for(int i = 0 ; i < len ; i++)
            {
                for (int j = i + 1 ; j < len ; j++)
                {
                    if (key[i] == key[j])
                    {
                        printf("Key must not contain repeated letters\n");
                        return 1;
                    }
                }
            }
            
            for ( int m = 'A'; m <= 'Z' ; m++)
            {
                key[m-'A'] = toupper(key[m-'A'])-m;
            }
            
            for (int k = 0,length=strlen(plaintext); k < length ; k++)
            {
                if(isalpha(plaintext[k]))
                {
                   plaintext[k]= plaintext[k]+key[plaintext[k]-(isupper(plaintext[k]) ? 'A' : 'a')];
                   
                }
                printf("%c",plaintext[k]);
            }
            
            printf("\n");
            
        }
        
        
        else
        {
           printf("Key must contain 26 letters\n");
            return 1; 
        }
        
        
        
    }
    
    else
    {
        printf("Usage: ./substitution key\n");
        return 1;
    }
    
    return 0;
}

bool check_validity_key(string s)
{
    int len = strlen(s) ;
    
    for(int i = 0 ; i < len ; i++)
    {
        if (!isalpha(s[i]))
        {
            return false;
        }
    }
    
    return true;
}

errors i get :

:( handles lack of key
    failed to execute program due to segmentation fault
:( handles invalid key length
    timed out while waiting for program to exit
:( handles invalid characters in key
    timed out while waiting for program to exit
:( handles duplicate characters in key
    timed out while waiting for program to exit
:( handles multiple duplicate characters in key
    timed out while waiting for program to exit