Bolded is where I am trying to have the program ignore spaces in plain text when it is outputted. I'm confused on how to do this. When I run the program, it does not ignore the spaces. Instead, it is ran as if the bolded else if statement is not present. I'm confused about why this is. I'm sorry if my code is a bit messy. I've just started programming.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int shift(char c);
int main(int argc, string argv[])
{
// Check to see if two arguments are enterted at launch
int cipher = 0;
if (argc != 2)
{
// If not return error & return 0
printf("Usage: ./vigenere keyword \n");
return 1;
}
else
{
int strlength = strlen(argv[1]);
// Iterates through characters in second argument (key), checking to see
// if they are digits
for (int k = 0; k < strlength; k++)
{
if (!isalpha(argv[1][k]))
{
// If not return error & return 1
printf("Usage: ./vigenere keyword\n");
return 2;
}
}
//char *c =argv[1];
string plaintext = get_string("Plaintext: ");
int len = (int)strlen(plaintext);
//int b = atoi(c);
char code[len];
strcpy(code, plaintext);
for (int j = 0; j < len; j++)
{
int key = shift(argv[1][j]);
if (isupper(argv[1][0]))
{
cipher = ((((code[j] - 'A') + key) % 26) + 'A');
//printf("%c", (((plaintext[j] - 'A') + key) % 26) + 'A');
//printf("%c",cipher);
}
else if (islower(argv[1][0]))
{
cipher = ((((code[j] - 'a') + key) % 26) + 'a');
//printf("%c", (((plaintext[j] - 'a') + key) % 26) + 'a');
printf("%c",cipher);
}
else if (!isalpha(code[j]))
{
code[j] = 0;
}
/* else
{
printf("%c", code[j] + (cipher));
}
*/
}
printf("\n");
}
}
int shift(char c)
{
int i = c;
if (i <= 'Z' && i >= 'A')
{
return ((i - 'A') % 26);
}
else
{
return ((i - 'a') % 26);
}
}