1

your help is appreciated and please feel free to critique my coding approach. My concern is, not only that the code isn't working, but that I maybe complicating this more than it needs to be.

Working on the Vigenere cipher, I have the following:

  • In this block of code, I wanted to change the ascii value of each of the alpha keyword chars to its alpha index value, so I can use the new value when encrypting the plain text.
if (isalpha(keyword[k]))
    {
        for (int k = 0, n = strlen(keyword); k < n; k++)
        {   
            // test - printf("The char in my keyword is %c, int value is %d\n", keyword[k], keyword[k]);

            if (islower(keyword[k]))
            {
                keyword[k] = keyword[k] - 'a';
            }

            if (isupper(keyword[k]))
            {
                keyword[k] = keyword[k] - 'A';
            }
         }
    }
    else
    {
        printf("Enter an alphabetic keyword\n");
        return 1;
    }
  • This is the code block that is supposed to encrypt the plaintext:

for (int i = 0, n = strlen(plainText); i < n; i++)

     {
        if (isalpha(plainText[i]))

             for (k = 0, n = (keyword[k] % strlen(keyword)); k < n; k++)
                if (islower(plainText[i]))
                {
                    cipherText = (((plainText[i] - 'a') + keyword) % 26 + 'a');
                    //printf("%c", cipherText);       
                }

                if (isupper(plainText[i]))
                {
                    cipherText = (((plainText[i] - 'A') + keyword) % 26 + 'A');
                    //printf("%c", cipherText);
                }

         printf("%c", cipherText);

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

It seems to me part of the problem is the way I'm using the variable keyword. I tried adding a new variable, like key, but that doesn't seem to work. Using printf statements as tests, I don't think the keyword is iterating as it should. Thanks in advance for your response!

1 Answer 1

4

While I haven't tested it, most of your code looks on target at first glance. However, you have a serious problem:

for (k = 0, n = (keyword[k] % strlen(keyword)); k < n; k++)

Your goal is to wrap back to the beginning of the keyword array (your key string) after you use the last element. Let's say that you have a 3 letter key. You want k to go from 0 to 1 to 2 and then back to 0, so you know to use the % modulo operator. BUT, what are you applying the modulo to? Look at it another way. Say you had a character array called foo. It contains "abc". That means foo[2] = 'c'. foo[2] % 3 translates to 'c' % 3 or 99 % 3 = 0.

By now, you probably realize that you need a variation of the current code. Try this:

for (k = 0, n = (keyword[k % strlen(keyword) ]  ); k < n; k++)

I'm not saying that everything will work, but that should get you rolling.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

5
  • Thanks! I'm still having issues with my keyword variable. I have it declared as string keyword = argv[1], which I've read in other posts on here is not a good idea. Any suggestions?
    – cignal
    Commented Jul 3, 2015 at 15:45
  • Why would you not want a string variable for keyword? It will contain a string made up of characters, right? Sounds like the perfect use of a string var. However, you need some help in loading it. Strings are not copied with an = operator. Instead, strings are manipulated with functions calls like strcpy(), strncpy(), strcmp(), strncmp(), etc. Single characters can be operated on with things like =, '+', or compared with ==, !=, etc.
    – Cliff B
    Commented Jul 3, 2015 at 17:28
  • Yes, that's true. I was looking at how to use the strcpy() function, but I'm not sure if using that function will fix the problem. I've noticed that my program seems to work -- encrypting each plaintext char, key skipping over non-alpha chars and repeating over all plaintext -- EXCEPT when there's an "a" in the key? If I use "a" or "A" as a key to encrypt "a" or "A", I get a "floating point exception (core dumped)" message. Any idea what might be causing this?
    – cignal
    Commented Jul 6, 2015 at 19:38
  • Don't know. At this point, you've almost certainly changed the code. You need to identify which line of code is causing the seg fault first. If it's the line that actually encodes the letter, try splitting it up into parts and do the encoding over several lines of code, with each line doing a small part of the process, and then see which part throws a seg fault. (You may have to run it with something other than 'a' to get it to work before you try with 'a', just to make sure the code still works.)
    – Cliff B
    Commented Jul 6, 2015 at 19:46
  • I finally got the program working. I had to take out the for loop I posted above that iterates through the keyword, it didn't like that for some reason. I made other changes but that was the main one, I think, that was causing the errors I was getting. Anyway, thanks Cliff B, your help is appreciated. Cheers!
    – cignal
    Commented Jul 15, 2015 at 19:17

You must log in to answer this question.

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