I've figured out how the caesar cipher should work in theory, i.e take away the ascii value of 'A', then add they key to the current number under consideration, mod it by 26 then re- add the value of 'A' in order to encipher > in the case of an uppercase letter and have most of the code, however, I think I may be missing something that is causing the output to contain strange characters such as '/'
Here is my code:
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
//ci = (pi + k) mod 26 ** pi is plaintext, the ith character in p, and k is key
{
//get the key from the user
if (argc != 2)
{
printf("Error, invalid input\n");
return 1;
}
//put key into int k
int k = atoi(argv[1]);
if (k < 0)
{
printf("Error, invalid input\n");
return 1;
}
else
{
//prompt user for the string to encode
//printf("\n");
string s = get_string();
//printf("%s",s);
//loops through length of string s
for (int i = 0, length = strlen(s); i < length; i++)
{
if (isalpha(s[i])) //checks if in alphabet
{
//printf("%c", s[i]);
if (isupper(s[i])) //returns true if upper case
{
printf("%c", (( s[i] - 'A' + k) % 26) + 'A');
}
if (islower(s[i])) //returns true if lower case
{
printf("%c", (( s[i] - 'a' + k) % 26) + 'a');
}
}
else
printf("%c", s[i]);
}
}
printf("\n");
return 0;
}