0

I've been at this for 2 days now and am completely stumped at this point. Absent of an 'a' (or 'A') in the key, the program works exactly as intended. If an 'a' is included in the key, however, the key only loops using the characters preceding it. For example: 'BCD' as the key will work perfectly, but 'BCAD' as the key will only loop using 'BC'. Any clue what the problem could be? Here's my code:

Thanks in advance!

// include libraries
      #include <stdio.h>
      #include <cs50.h>
      #include <string.h>
      #include <ctype.h>

      int main(int argc, string argv[])
      {
        // verify that user has input only two command-line arguments
        if (argc == 2)
        {
            // traverse through characters in key
            for(int count = 0, arglen = strlen(argv[1]); count < arglen; count++)
            {
                // verify that key is in alphabetical characters only
                if (isalpha(argv[1][count]))
                {
                // equalize alpha-key values to 0+
                if (isupper(argv[1][count]))
                {
                    argv[1][count] = argv[1][count] - 'A';
                }
                else
                {
                  if (islower(argv[1][count]))
                    {
                        argv[1][count] = argv[1][count] - 'a';
                    }
                }
                }
                else
                {
                // display error message if any non-alphabetical characters were used in key
                printf("Please enter alphabetical characters only.\n");
                return 1;
                }
            }
        }
        else
        {
          // display error message if more or less than two command-line arguments
          printf("Please enter two command-line arguments only.\n");
          return 1;
        }
        // ask user for input
        printf("plaintext: ");
        string text = get_string();
        // safety check for acceptable input
        if (text != NULL)
        {
            printf("ciphertext: ");
            // traverse through characters in input
            for(int txtcount = 0, txtlen = strlen(text), keycount = 0, arglen = strlen(argv[1]); txtcount < txtlen; txtcount++)
            {
                // check for alphabetic characters
                if (isalpha(text[txtcount]))
                {
                    // check for upper-case letters
                    if (isupper(text[txtcount]))
                    {
                        // encrypt upper-case letters
                        int encrypted = text[txtcount] + argv[1][keycount];
                        keycount++;
                        // check for encrypting past 'Z'
                        if (encrypted >= 91)
                        {
                          // wrap around upper-case alphabet
                          encrypted = 65 + (encrypted % 91);
                          printf("%c", encrypted);
                        }
                        else
                        {
                          printf("%c", encrypted);
                        }
                        // check for incrementing past key legnth
                        if (keycount >= arglen)
                        {
                          // wrap around key
                          keycount = 0;
                        }
                    }
                    else
                    {
                      // check for lower-case letters
                      if (islower(text[txtcount]))
                      {
                          // encrypt lower-case letters
                          int encrypted = text[txtcount] + argv[1][keycount];
                          keycount++;
                          // check for encrypting past 'z'
                          if (encrypted >= 123)
                          {
                            // wrap around lower-case alphabet
                            encrypted = 97 + (encrypted % 123);
                            printf("%c", encrypted);
                          }
                          else
                          {
                            printf("%c", encrypted);
                          }
                          // check for incrementing past key legnth
                          if (keycount >= arglen)
                          {
                          // wrap around key
                          keycount = 0;
                          }
                      }
                    }
                }
                else
                {
                  // display non-alphabetic characters w/o encryption
                  printf("%c", text[txtcount]);
                }
            }
            printf("\n");
            return 0;
        }
      }
1

Interesting interaction between different parts of the code. When there is an 'a' or an 'A' in the key it will fail because of a unique interaction in your code. The code first converts all of the key chars to numbers between 0 and 25 inclusive. The problem lies in the conversion of the a's to 0.

Later in the code, the following code appears in the for loop setup:

for ( ...   arglen = strlen(argv[1])   ...)

The problem with this originates in how the strlen function works. The strlen function looks for the end of string marker, a zero byte, to determine the string length. Since any 'a' is converted to 0, it is being interpreted as the end of string marker. The key length stored is thus incorrect.

If you capture the key string length before converting the chars and store it in a var for use later, it'll cure this problem.

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

1
  • You're the man, Cliff! Worked like a charm. Great explanation of the problem too. I might have never caught that. Many thanks 🙏 – Foxhole Jul 30 '17 at 0:38

You must log in to answer this question.

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