0
#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
1

Maybe this link is helpful: Do YOU know how to find a seg fault?? Advice to new programmers

I've encountered that problem more than one time, too

| improve this answer | |
  • I got it to work. Appreciate you – Mathew Olajide May 12 at 1:26
  • Very good. What was the exact problem? – ALL May 12 at 4:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .