Skip to content

Instantly share code, notes, and snippets.

@baktun95827
Forked from CraigRodrigues/caesar.c
Created December 24, 2017 19:18
Show Gist options
  • Select an option

  • Save baktun95827/b030ac9c95f26a0a73f217f476686627 to your computer and use it in GitHub Desktop.

Select an option

Save baktun95827/b030ac9c95f26a0a73f217f476686627 to your computer and use it in GitHub Desktop.

Revisions

  1. @CraigRodrigues CraigRodrigues revised this gist Jun 1, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions caesar.c
    Original file line number Diff line number Diff line change
    @@ -40,9 +40,9 @@
    {
    //check if the letter is uppercase or lowercase then convert
    if islower(code[i])
    printf("%c", (((code[i] + k) % 96) % 26) + 96);
    printf("%c", (((code[i] + k) - 97) % 26) + 97);
    else if isupper(code[i])
    printf("%c", (((code[i] + k) % 64) % 26) + 64);
    printf("%c", (((code[i] + k) - 65) % 26) + 65);
    //if neither then just print whatever it is
    else
    printf("%c", code[i]);
  2. @CraigRodrigues CraigRodrigues revised this gist May 31, 2016. No changes.
  3. @CraigRodrigues CraigRodrigues created this gist May 31, 2016.
    53 changes: 53 additions & 0 deletions caesar.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    #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) % 96) % 26) + 96);
    else if isupper(code[i])
    printf("%c", (((code[i] + k) % 64) % 26) + 64);
    //if neither then just print whatever it is
    else
    printf("%c", code[i]);
    }
    printf("\n");
    return 0;
    }
    }