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

int main(int argc, string argv[])
{
     if (argc > 2 || argc == 1)
     {
        printf("Please put appropriate cl arguement!\n");
        return 1;
     }

int k = strlen(argv[1]); // store string length of key in k
string j = argv[1]; // introduce string j to use individual characters of key later
string pt = get_string("plaintext: "); // prompt for plaintext
char ct[strlen(pt)]; // array of characters for ciphertext
for (int i = 0, h = 0; i < strlen(pt); i++) // loop to encipher and introduce h to skip the enciphering for non-alphabet chars
{
    if (isupper(pt[i]) && isupper(j[(i - h)]))
    {
        int a = ((pt[i] - 65) + ((j[(i - h) % k] - 65))) % 26; // enciphering and ASCII to alphabetic index stored in a
        ct[i] = 65 + a; // Alphabetic index to ASCII and stored in ith element of array ct
    }
    else if (isupper(pt[i]) && islower(j[(i - h)]))
    {
        int a = ((pt[i] - 65) + ((j[(i - h) % k] - 97))) % 26;
        ct[i] = 65 + a;
    }
    else if (islower(pt[i]) && isupper(j[(i - h)]))
    {
        int a = ((pt[i] - 97) + ((j[(i - h) % k] - 65))) % 26;
        ct[i] = 97 + a;
    }
    else if (islower(pt[i]) && islower(j[(i - h)]))
    {
        int a = ((pt[i] - 97) + ((j[(i - h) % k] - 97))) % 26;
        ct[i] = 97 + a;
    }
    else
    {
        ct[i] = pt[i]; // no change when char is not alphabet
        h = h + 1; // h stores number of non-aphabet characters before ith character

    }
}
printf("ciphertext: %s\n", ct);
return 0;

}

When executing:

   ./vinegere bacon
   plain text: Meet me at the park at eleven am
   ciphertext: Negh ze at the park at eleven am 

Please help.

1 Answer 1

1

The problem is in the names of your variables: As most of the names have no meaning, it is difficult to keep track of the meaning for each of these variables.

In this case, your variable h makes things too complicated, that's probably why you lost track.

You are iterating over the characters of the input and over the characters of the key. But you are trying to do that using a single loop variable. And you try to correct the second (missing) loop variable by using h. And at the same time you have to keep remembering that you have to cycle back to the beginning of the key whenever you reach the end of the key...

I think that design is a disaster waiting to happen.


The reason this actually failed is because you forgot one of these things. And unfortunately, that part occurs 4 times in this code, as far as I can see.

In your current design, you'll have to remember that you add % k whenever you compute i - j (which in fact should have been your second loop variable, or in other words: a variable which keeps track which of the characters of the key should be used). Currently, you are computing that same thing (approximately) 9 times, and you forget the % k (approximately) 4 times. That is why your code stops working after a couple of characters ...


Finally, to quote Cliff B:

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

6
  • Thank you, So I got that variables are confusing and I should deal with it but last couple of lines of your answer completely went over my head, that 9 times and 4 times thing and I still don't what to fix inorder to make it work :( Oct 18, 2018 at 12:11
  • Easiest way to solve it at this moment would probably to replace both instances of isupper(j[(i - h)] with isupper(j[(i - h) % k] and to do the same thing with islower. But than you would do exactly the same computation 9 times ... Oct 18, 2018 at 12:24
  • A better way to patch this would be to create a new variable, and fill it with (i - j) % k. Than you can simply use that variable 9 times. Oct 18, 2018 at 12:25
  • 1
    Thanks, you are a life saver, it really worked and now I got why it was not working before because value of '(i - h)' was going beyond the number of characters in string j for which '%k' was required. Thank you :) Oct 18, 2018 at 12:43
  • Hey one problem now, sometimes it is now outputting extra characters at end :/ Oct 18, 2018 at 12:49

You must log in to answer this question.

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