-2

My code for CS50 pset2 Vigenere cypher is as follows. Please help me find the bug. This code basically does nothing when the key includes the letter 'a'. It says "Floating Point Exception". I don't know what that means. Please go through the code and tell me what the bug is.

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

int main(int argc, string argv[])
{
    //check if there are only two command line arguments//

    if (argc!=2)
    {
        printf("Please enter a valid input!\n");
        return 1;
    };

    string key = argv[1];

    for(int i=0, n=strlen(key); i < n; i++)
    {
        //check if there is any number in the key//

        if(!isalpha(key[i]))
        {
            printf("Invalid Key!\n");
            return 1;
        };

        //converting the key into ints.//

        if(islower(key[i]))
        {
            key[i] = key[i] - 'a';
        }
        else if(isupper(key[i]))
        {
            key[i] = key[i] - 'A';
        };
    };

    //prompt user for the string//

    string s = GetString();

    int c;
    int k;
    int stln = strlen(s);
    int kyln = strlen(key);          

    for(int j = 0, m = strlen(s); j < m; j++)
    {
        if(islower(s[j]))
        {
            s[j] = s[j] - 'a';

            //for wrapping around the key//

            if(stln > kyln)
            {
                k = j % strlen(key);
                c = (s[j] + key[k]) % 26;
                s[j] = 'a' + c;
            } 
            else
            {
                c = (s[j] + key[j]) % 26;
                s[j] = 'a' + c;
            };
         } 
         else if (isupper(s[j]))
         {
             s[j] = s[j] - 'A';

             //for wrapping around the key//

             k = j % strlen(key);

             if(stln > kyln)
             {
                 c = (s[j] + key[k]) % 26;
                 s[j] = 'A' + c;
             } 
             else
             {
                 c = (s[j] + key[j]) % 26;
                 s[j] = 'A' + c;
             };
         };

    };

    printf("%s\n", s);

};
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Yudhishthir Singh
  • 2,941
  • 2
  • 23
  • 42
  • Show definition of `string`. – chux - Reinstate Monica Jul 06 '16 at 20:45
  • 4
    Off topic: you don't need semicolons after closing curly braces. In other words `};` should be `}` – user3386109 Jul 06 '16 at 20:48
  • chux, the cs50 library included defines string. It is just typedef of char* http://mirror.cs50.net/library50/c/cs50-library-c-3.0/cs50.h – MacedonZero Jul 06 '16 at 20:55
  • @MacedonZero Thanks for `typedef char *string;` For a long time I thought a _string_ was a pointer in C - much like that `typedef`. Instead C defines a _string_ as an array of characters up to the null character. That more correct definition solved a lot of problems and improved understanding. `typedef char *string_ptr;` would be more educational. – chux - Reinstate Monica Jul 06 '16 at 21:12

1 Answers1

3

strlen(key) is invalid in later code as earlier code was injecting null characters in key[] whenever an 'a' or 'A' was used:

if(islower(key[i])) {
  key[i] = key[i] - 'a';

Find the string length of key[] before changing its contents and use that

 int n = strlen(key);
 ...
 // k = j % strlen(key);
 k = j % n;
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256