Search code examples
vigenerefloating-point-exceptions

Vigenere.c CS50 Floating Point Exception (Core Dumped)


I am working on the Vigenere exercise from Harvard's CS50 (in case you noticed I'm using string and not str).

My program gives me a Floating Point Exception error when I use "a" in the keyword.

It actually gives me that error

  • when I use "a" by itself, and
  • when I use "a" within a bigger word it just gives me wrong output.
  • For any other kind of keyword, the program works perfectly fine.

I've run a million tests. Why is it doing this? I can't see where I'm dividing or % by 0. The length of the keyword is always at least 1. It is probably going to be some super simple mistake, but I've been at this for about 10 hours and I can barely remember my name.

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

int main (int argc, string argv[])
{        
    //Error message if argc is not 2 and argv[1] is not alphabetical
    if (argc != 2)
    {
        printf("Insert './vigenere' followed by an all alphabetical key\n");
        return 1;
    } 
    else if (argv[1])
    {
        for (int i = 0, n = strlen(argv[1]); i < n; i++)
        {
            if (isalpha((argv[1])[i]) == false)
            {
                printf("Insert './vigenere' followed by an all alphabetical key\n");
                return 1;
            }
        } 

        //Store keyword in variable
        string keyword = argv[1];        

        //Convert all capital chars in keyword to lowercase values, then converts them to alphabetical corresponding number
        for (int i = 0, n = strlen(keyword); i < n; i++)
        {
            if (isupper(keyword[i])) {
                keyword[i] += 32; 
            } 
            keyword[i] -= 97;

        }

       //Ask for users message
        string message = GetString();

        int counter = 0;
        int keywordLength = strlen(keyword);

         //Iterate through each of the message's chars
        for (int i = 0, n = strlen(message); i < n; i++)
        {

            //Check if ith char is a letter
            if (isalpha(message[i])) {                                            

                int index = counter % keywordLength;

                if (isupper(message[i])) {

                    char letter = (((message[i] - 65) + (keyword[index])) % 26) + 65;

                    printf("%c", letter);                    
                    counter++;

                } else if (islower(message[i])) {

                    char letter = (((message[i] - 97) + (keyword[index])) % 26) + 97;

                    printf("%c", letter);                    
                    counter++;                    
                } 

            } else {

                    //Prints non alphabetic characters
                    printf("%c", message[i]);  
            }                             

        }        
        printf("\n");        
        return 0;
    }   
}

Solution

  • This behavior is caused by the line keyword[i] -= 97;, there you make every 'a' in the key stream a zero. Later you use strlen() on the transformed key. So when the key starts with an 'a', keywordLength therefor is set to zero, and the modulo keywordLength operation get into a division by zero. You can fix this by calculating the keyword length before the key transformation.