0

I'm having trouble to reset the j'th value to zero whenever we use all the letters of the key. This code prints a weird conglomerate of symbols smth like ( {[{Y}]}{[Y}]} ... ). Thanks for the help!

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

int main (int argc, string argv[])
{
    if (argc != 2)
    {
        printf ("Usage: ./caesar key , Ex: ./caeasar ABC (key must be 
        alphabetical)\n");
        return 1;
    }

int keyCheck;
for (int k = 0; k < strlen(argv[1]); k++)
{
    keyCheck = argv[1][k];
    if(!isalpha(keyCheck))
    {
       printf ("Usage: ./caesar key , Ex: ./caeasar ABC (key must be alphabetical)\n");
       return 1; 
    }
}

printf ("plaintext: ");
string plaintext = get_string();

printf ("ciphertext: ");
for (int j = 0; j < strlen(argv[1]); j++)
{
    int alphaKey;
    int currKey = argv[1][j];
    if (isupper(currKey))
    {
        alphaKey = currKey - 65;
    }
    else if (islower(currKey))
    {
        alphaKey = currKey - 97;
    }

    for (int i = 0; i < strlen(plaintext); i++)
    {
        int currChar = plaintext[i];
        int alphaOrder;
        int cipherVal;
        int asciiVal;

        if (isalpha(currChar))
        {
            if (isupper(currChar))
            {
                alphaOrder = currChar - 65;
                cipherVal = alphaOrder + alphaKey;
                asciiVal = cipherVal + 65;
                printf ("%c", asciiVal);
            }
            else
            {
                alphaOrder = currChar - 97;
                cipherVal = alphaOrder + alphaKey;
                asciiVal = cipherVal + 97;
                printf ("%c", asciiVal);     
            }
        }
        else 
            {
                printf ("%c", currChar);
            }
    }
    if (j % (strlen(argv[1]) - 1) == 0)
    {
        j = 0;
    }
  }
}

1 Answer 1

1
   if (j % (strlen(argv[1]) - 1) == 0)

Why are you subtracting 1? That deals with correcting the reset to 0.

However, there is a serious logic problem in the code. Simply put, nested for loops simply don't work here. It will end up going through the plain text j times (it could also potentially turn into an infinite loop when j is reset to 0). The code needs to make one pass through the plain text and needs to consume the key chars only when an alpha is encoded.

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

4
  • yeah it basically created an infinite loop..., so I should write the for loop when alpha is encoded? What does that mean?
    – DCBuilder
    Apr 26, 2017 at 18:17
  • Hey I've tried to do what you told me but I can't really figure out how to do what you told me to do. "The code needs to make one pass through the plain text and needs to consume the key chars only when an alpha is encoded." I've spent the last hour doing what you told me to do, but I doesn't seem to find a correct fix to my code. I keep getting infinite loops...
    – DCBuilder
    Apr 26, 2017 at 18:43
  • I expected the infinite loops. It's simple. Get rid of the outer for loop. Create an int var to be used as the index for the key, let's say j. When a letter is encoded, j=(j+1)%strlen(key); Once you fix it, you should go back and walk through the erroneous code and understand why you were getting infinite loops. That will teach you so much more!
    – Cliff B
    Apr 26, 2017 at 19:54
  • Now I understand! Thx!
    – DCBuilder
    Apr 27, 2017 at 19:16

You must log in to answer this question.

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