0

The code executes but simply prints out the wrong output. For example, when I provide a key of 'a' and then ask for 'aaaa' to be converted, the code prints out 'cccc' instead of 'aaaa'. I can't figure out what I've done wrong, although I do believe it is something with my formula for cyphering c (i.e. c = ((c+k-65)%26 + 65). However when I look at it I can't see any error. Can anyone explain to me why my formula is incorrect?

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

int main(int argc, char* argv[])
{
    //check for presence of command line argument
    if (argc<2)
    {
        printf ("Encryption failed, no key provided\n");
        return 1;
    }

    //check if key is a character
    if (isalpha(*argv[1])==0)
    {
        printf("Encryption failed. Please provide character key.\n");
        return 1;
    }

    //convert key to integer value
    int k;
    if (isupper(*argv[1]) != 0)
    {
        k = atoi(argv[1])-65;
    }
    if (islower(*argv[1]) != 0)
    {
        k = atoi(argv[1])-97;
    }

    //ask for phrase to be encrypted
    printf ("Please enter a phrase to be encrypted: ");
    string p = GetString();
    int L = strlen(p);

    for (int i=0; i<L; i++)
    {
        char c = p[i];
        if(isalpha(c)==0) //if not a letter...
        {
            printf("%c", c);
        }
        if(isupper(c)!=0)    //if uppercase...
        {
            c = (c+k-65)%26 + 65;
            printf("%c", c);
        }
        if (islower(c)!=0)   //if lowercase...
        {
            c = (c+k-97)%26 + 97;
            printf("%c", c);
        }
    }

    printf ("\n");
    printf ("Encryption complete\n");
    return 0;
}
0

Two serious problems. First, you misread the assignment. Caesar is supposed to accept a number as the key, not a letter as you have done. But save your work, it will be useful on Vigenere.

As for why your code doesn't work, look at your encoding:

c = (c+k-65)%26 + 65;

The modulo only works if both the letter and the key are converted from ASCII to numbers between 0 and 25 inclusive. By subtracting 65 only once, you only converted one of them, not both. (We won't even mention the problem of mixed cases.)

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

3
  • Thanks for your reply. I did indeed misread the assignment, but I want to figure this out anyways. Earlier in the function I had already subtracted 65 from k. So they should both be converted. – noggy Nov 7 '15 at 3:56
  • Hmmm, overlooked that. But.... well, no, you're not. atoi(argv[1]) = 0. So that means that k = - 97 or - 65. You can do direct math on chars, so instead, it could have been k = argv[1][0] - 95; – Cliff B Nov 7 '15 at 4:15
  • Oh so does the function automatically assume that argv[1] is an array? Otherwise why is argv[1][0] necessary? I tried doing this but the output is still incorrect; a key of 'a' encrypts 'aaaa' as 'NNNN' – noggy Nov 7 '15 at 5:41

You must log in to answer this question.

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