0

error message: rejects https://sandbox.cs50.net/checks/742ebfdf355b45d594e8f23047837f51

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

int main (int argc, string argv[])
{
    if(argc != 2)

    {
        printf("type: ./vigenere key\n");
        return 1;
    }

    string key = argv[1];
    for(int i = 0, n = strlen(key); i < n; i++)
    {
        if (!isalpha(key[i]))
        {
            printf("type program name followed by non-numeric key\n");
            return 1;
        }
    }
    //takes user plain text to encrypt
    //printf("plaintext:  ");
    string text = GetString();
    int j = 0, k_len = strlen(key);

    //printf("ciphertext:  ");
   for(int i = 0, n = strlen(text); i < n; i++)

        if(isalpha(text[i]))
        {
            if(isupper(text[i]))
            {
                char cipher = (text[i] - 'A' + key[j % k_len] - 'A') % 26; 
                j++;
                printf("%c", cipher + 'A');

            }
            if(islower(text[i]))
            {
                char cipher = (text[i] - 'a' + key[j % k_len] - 'a') % 26; 
                j++;
                printf("%c", cipher + 'a');

            }

        }
        else
        {
            printf("%c", text[i]);

        }

     printf("\n");
}

1 Answer 1

0

How does your code handle the case where the plain text is lower case and the key is upper case, or the opposite?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • oh, I didn't realize that I was supposed to consider the capitalization of the key! Although this was helpful I'm still stuck in implementing the if loop for the key upper/lower case:
    – Jana B.W.
    Aug 7, 2016 at 18:57

You must log in to answer this question.

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