0

I'm working on my Vigenere Cipher and I cannot get past a segmentation fault. From my short experience I've noticed I get these when a loop isn't able to complete, but I don't think mine is able to begin the loop so I'm a little lost.

Anyway, heres my code. When I make my !isalpha read null, it fails properly. When I make it read properly (I believe so anyway) I get a segmentation fault.

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


       int i;
       int main(int argc, string argv[])
       {
       if (argc != 2)
          {
           printf("Enter only one WORD\n");
            return(1);
           }
       if (!isalpha(argv[1]))
          {
          printf("Enter only PLAIN TEXT\n");
          return(1);
          }
      else

    {



    string text = GetString();

    for (int i = 0; i < strlen(text); ++i)
        {
            int y = 0; 
            int max = strlen(argv[1]);
            if (isupper(text[i]))
            {
                 int m = argv[1][y];
                 m = m - 65;
                 m = (m % max);
                 y++;
                 if (y == strlen(argv[1]))
                    {
                    y = 0;
                    }



                 int j = text[i];
                 j = j - 65;
                 j = j + m;
                 j = ((j % 26) + 65);

                 char l = j;
                 printf("%c", l);
            }

             if (islower(text[i]))
            {
                int m = argv[1][y];
                 m = m - 65;
                 m = (m % max);
                 y++;
                 if (y == strlen(argv[1]))
                    {
                    y = 0;
                    }


                int j = text[i];
                j = j - 97;
                j = j + m;
                j = ((j % 26) + 97);

                char l = j;
                printf("%c", l);
            }
            if (isspace(text[i]))
                {
                printf("%c", text[i]);
                }

            if (ispunct(text[i]))
                {
                printf("%c", text[i]);
                }
    }
}
printf("\n");
}

1 Answer 1

1

Actually, a seg fault is usually an attempt to access memory that hasn't been set up or allocated by your program. In your case, it's caused by the second test in your program:

   if (!isalpha(argv[1]))

Check out the documentation on isalpha(). It takes a single character as a parameter. You are trying to feed it an entire string, so it chokes and spits it back out as a seg fault! Remember, argv[1] is a string, not a char. To do what you want, you need to cycle through the string, testing each character one at a time, say, with a for loop. To access each character, you need two indexes argv[1][i] because this is essentially a two dimensional array.

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. ;-)

You must log in to answer this question.

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