If someone has the time to explain to me how I can fix my code that would be greatly appreciated! Thanks :)
Problems that I need help with:
1 - Program needs to accept single character inputs like 'a' and output a single ciphered character (not followed by random symbols)
2 - Program needs to accept string with multiple punctuations like 'world, say hello!; and output a ciphered string (not followed by random symbols)
Here is check50's feedback:
:) caesar.c exists.
:) caesar.c compiles.
:( encrypts "a" as "b" using 1 as key output not valid ASCII text
:) encrypts "barfoo" as "yxocll" using 23 as key
:) encrypts "BARFOO" as "EDUIRR" using 3 as key
:) encrypts "BaRFoo" as "FeVJss" using 4 as key
:) encrypts "barfoo" as "onesbb" using 65 as key
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key output not valid ASCII text
:) handles lack of argv[1]
:) handles non-numeric key
:) handles too many arguments
Here is my code:
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
// Declare only 2 arguments because argc[0] is name of problem, so argc[1] is input of human
if (argc != 2)
{
printf("Usage: ./caesar Please enter a cipher key\n"); //Provide this 'error message' if the human does not enter a cipher key
return 1;
}
// Check if input by human is a digit (assuming only positive digit)
string string_key = argv[1];
for (int i = 0, n = strlen(string_key); i < n; i++)
{
if (isdigit(string_key[i]) == false)
{
printf("Usage: ./caesar Please enter an integer for the cipher key\n"); //Provide this 'error message' if the human entered a cipher key that isn't a digit
return 1;
}
}
// If input by human is a digit, then CONVERT the plain text string_key to an int
int key = atoi(string_key);
// Prompt human to entere a plaintext for a string to encipher
string plaintext = get_string("plaintext: ");
//Declare variables to determine upper and lower case differences
int uppercase = 65;
int lowercase = 97;
// Declare variable that gets length of plaintext
int text_length = strlen(plaintext);
// Create the ciphertext array with the length of the plaintext
char ciphertext[text_length];
// For loop to check plaintext
char charactercheck;
for (int i = 0; i < text_length; i++)
{
charactercheck = plaintext[i];
if (isalpha(charactercheck)) // this checks if the character is a letter
{
if (isupper(charactercheck)) // this checks if the character is uppercase
{
charactercheck = charactercheck - uppercase; //convert to alpha values
charactercheck = (charactercheck + key) % 26; //shift to key
charactercheck = charactercheck + uppercase; //convert back to ASCII values
ciphertext[i] = charactercheck; //add to ciphertext
}
else
{
charactercheck = charactercheck - lowercase; //convert to alpha values
charactercheck = (charactercheck + key) % 26; //shift to key
charactercheck = charactercheck + lowercase; //convert back to ASCII values
ciphertext[i] = charactercheck; //add to ciphertext
}
}
else
{
ciphertext[i] = charactercheck; //Add to ciphertext
}
}
printf("ciphertext: %s\n", ciphertext); //Output the ciperhered text
}