I have been working on this since yesterday and after much struggle have managed to encrypt the message successfully. However, my output is missing spaces.
As I understand it, the reason this is happening is because I'm using isalpha(), isupper() and islower() commands and as a result am ignoring the spaces in the original input.
Can one please help me with how to retain the original spaces and punctuation?
Below is my code- its far from elegant and any comments on style will also be appreciated!
(Also, while there are plenty of questions on Caesar Cipher, none barring one deal with this problem. Since this is my first week programming, I had trouble understanding the syntax in that one.)
There is a blatant mistake in my algorithm which causes it to output the wrong values if given certain arguments. For instance with a k of 13, inputting anything after the 13th alphabet (m, I think) will output something very bizarre. I will amend this and get back soon! Till then, take my code with a grain of salt!
# include <cs50.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <ctype.h>
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Please enter a valid number of arguments! \n");
return 1;
}
string num = argv[1];
int k = atoi(num);
if (k < 0)
{
printf("Please enter a valid number! \n");
return 1;
}
printf("Please type the message which needs to be encrypted: ");
string p = GetString();
for (int i = 0, n = strlen(p); i < n; i++)
{
int oldletter = p[i];
int result1 = (oldletter + k);
int result2 = (oldletter - 65 + k);
int result3 = (result2) % 26;
int result4 = (oldletter - 97 + k);
int result5 = (result4) % 26;
if (isalpha(p[i]) && isupper(p[i]) && k < 26)
{
printf("%c", result1);
}
if (isalpha(p[i]) && isupper(p[i]) && k >= 26)
{
int result7 = (result3 + oldletter);
printf("%c", result7);
}
if (isalpha(p[i]) && islower(p[i]) && k < 26)
{
printf("%c", result1);
}
if (isalpha(p[i]) && islower(p[i]) && k >= 26)
{
int result8 = (result5 + oldletter);
printf("%c", result8);
}
}
printf("\n");
}
Corrected code, working properly: SPOILERS AHEAD
# include <cs50.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <ctype.h>
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Please enter a valid number of arguments! \n");
return 1;
}
string num = argv[1];
int k = atoi(num);
if (k < 0)
{
printf("Please enter a valid number! \n");
return 1;
}
printf("Message: ");
string p = GetString();
for (int i = 0, n = strlen(p); i < n; i++)
{
int oldletter = p[i];
int result1 = (oldletter - 65 + k);
int result2 = (result1) % 26;
int result3 = (oldletter - 97 + k);
int result4 = (result3) % 26;
if (isalpha(p[i]) && isupper(p[i]))
{
int result5 = (result2 + 65);
printf("%c", result5);
}
else if (isalpha(p[i]) && islower(p[i]))
{
int result6 = (result4 + 97);
printf("%c", result6);
}
else
{
printf("%c", p[i]);
}
}
printf("\n");
}