Skip to content

Instantly share code, notes, and snippets.

@Mokka68
Created January 23, 2018 02:11
Show Gist options
  • Save Mokka68/5b97388ab6991649d933e5236de7a293 to your computer and use it in GitHub Desktop.
Save Mokka68/5b97388ab6991649d933e5236de7a293 to your computer and use it in GitHub Desktop.
this is my solution to Caesar in CS50
/**
* Caesar50.c
* This program encrypts texts using Caesar's cipher.
* by shiftting the letters k times according to what the user inputs.
* the command line arguments should be only 2 arguments otherwise it won't work
* */
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, string argv[])
{
//check if the arguments are two
if (argc != 2)
{
printf("can not be ciphered\n");
return 1;
}
// declaring that the value of k is the interger value of the second (string) argument
int k = atoi(argv[1]);
if (k < 0)
{
printf("please enter a valid number\n");
return 1;
}
//prompt the user for the text
string text = get_string("please Enter your text: ");
printf("ciphertext:");
for (int i = 0, n = strlen(text); i < n; i++)
{
//check if the character is a letter
if (isalpha(text[i]))
{
//check if the letter is uppercase
if (isupper(text[i]))
{
//converting the letter to number and cipher it
char cipher_num_capital = ((text[i] - 65 + k) % 26) + 65;
//print the letter of the number as a character
printf("%c", cipher_num_capital);
}
//check if the letter is lowercase
if (islower(text[i]))
{
//converting the letter to number and cipher it
char cipher_num_small = ((text[i] - 97 + k) % 26) + 97;
//print the letter of the number as character
printf("%c", cipher_num_small);
}
}
else
{
//in case if the character in not a letter then print it as it is
printf("%c", text[i]);
}
}
printf("\n");
}
@magneto07
Copy link

do you guys know what mistake i made for the result to be segmentation fault?

#include <string.h>
#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, string argv[])
{
// check if argc is 2
if (argc == 2)
{
printf ("Error, cannot be ciphered\n");
return 1;
}

// check if k is integer for 2nd argc
int k = atoi(argv[1]);
if (k < 0)
{
    printf("Usage: ./caesar key\n");
    return 1;
}

// ask user for plaintext
string s = get_string("Enter plaintext: ");

printf("Ciphertext:");

for (int i = 0, n = strlen(s) ; i < n; i++)

{
    // check if string is letter
    if (isalpha(s[i]))
    {
        //check if the letter is uppercase
        if (isupper(s[i]))
        {
            //converting the letter to number and cipher it
            char cipher_num_capital = ((s[i] - 65 + k) % 26) + 65;
            
            //print the letter of the number as a character
            printf("%c", cipher_num_capital);

        }

        //check if the letter is lowercase
        if (islower(s[i]))
        {
            //converting the letter to number and cipher
            char cipher_num_small = ((s[i] - 97 + k) % 26) + 97;
            
            //print the letter of the number as character
            printf("%c", cipher_num_small);
        }


    }
    else
    {
        // if the character in not a letter then print it as it is
        printf("%c", s[i]);
    }
}
printf("\n");

}

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