Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues
Last active October 1, 2022 02:57
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CraigRodrigues/100ab759b6b6722ba3ad8acf12e26371 to your computer and use it in GitHub Desktop.
Save CraigRodrigues/100ab759b6b6722ba3ad8acf12e26371 to your computer and use it in GitHub Desktop.
My solution to CS50 pset2 - "Hail, Caesar!"
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
/**
* Caesar.c
* A program that encrypts messages using Caesar’s cipher. Your program must
* accept a single command-line argument: a non-negative integer. Let’s call it
* k for the sake of discussion. If your program is executed without any
* command-line arguments or with more than one command-line argument, your
* program should yell at the user and return a value of 1.
*
* */
int main(int argc, string argv[])
{
// check for 2 arguments only
if (argc != 2)
{
printf("Nope\n");
return 1;
}
// once I check for correct argv put key into an int k
int k = atoi(argv[1]);
// check if the integer is non-negative
if (k < 0)
{
printf("Nope\n");
return 1;
}
else
{
// prompt user for a code to encrypt
string code = GetString();
for (int i = 0, n = strlen(code); i < n; i++)
{
//check if the letter is uppercase or lowercase then convert
if islower(code[i])
printf("%c", (((code[i] + k) - 97) % 26) + 97);
else if isupper(code[i])
printf("%c", (((code[i] + k) - 65) % 26) + 65);
//if neither then just print whatever it is
else
printf("%c", code[i]);
}
printf("\n");
return 0;
}
}
@ClaudioCS50
Copy link

ClaudioCS50 commented Jan 27, 2022

Guys I'm getting this:

:) caesar.c exists.
:) caesar.c compiles.
:( encrypts "a" as "b" using 1 as key
expected "ciphertext: b...", not "ciphertext: b..."
:( encrypts "barfoo" as "yxocll" using 23 as key
expected "ciphertext: yx...", not "ciphertext: yx..."
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
expected "ciphertext: ED...", not "ciphertext: ED..."
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
expected "ciphertext: Fe...", not "ciphertext: Fe..."
:( encrypts "barfoo" as "onesbb" using 65 as key
expected "ciphertext: on...", not "ciphertext: on..."
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
expected "ciphertext: ia...", not "ciphertext: ia..."
:) handles lack of argv[1]
:) handles non-numeric key
:) handles too many arguments

but the output match.... Any idea?

@bellatorsoprano
Copy link

idk
i'm also getting thi with similiar code

were u able to do it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment