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

int main(int argc, string argv[])
{
string k = argv[1];
if (argc == 2 && isalpha(k))
{
printf("plaintext: ");
string p = get_string();
printf("ciphertext: ");

for(int i=0, j = 0, n = strlen(p); i< n; i++, j++)
{
   if (isupper(p[i]) && isupper(k[j % strlen(k)]))
  {
      printf("%c", (p[i] + (k[j % strlen(k)] - 65)));
  }
   if (islower(p[i]) && isupper(k[j % strlen(k)]))
  {
      printf("%c", (p[i] + (k[j % strlen(k)] - 65)));
  } 
   if (isupper(p[i]) && islower(k[j % strlen(k)]))      
  {
      printf("%c", (p[i] + (k[j % strlen(k)]  - 97)));
  }
   if (islower(p[i]) && islower(k[j % strlen(k)]))
  {
      printf("%c", (p[i] + (k[j % strlen(k)]  - 97)));
  }
  if (isspace(p[i]))
  {
    printf(" ");
  }
  if (ispunct(p[i]))
  {
    printf("%c", p[i]);
  }

}
printf("\n");
}    
   else
    {
     printf("Invalid Key\n");
     return 1;
     }
 }

This is my code for the vigenere cypher from the pset2 of CS50 course.

I get a segmentation fault at run time. But it goes away if I omit the && isalpha(p[i]) in line 10, the program runs with no problem. What is causing this fault ?

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57

0 Answers0