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");
}