Skip to content

Instantly share code, notes, and snippets.

@AbdelrahmanSherifHadeya
Last active August 24, 2018 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbdelrahmanSherifHadeya/ce5793ad79b67e8845e7e6edad1a64f7 to your computer and use it in GitHub Desktop.
Save AbdelrahmanSherifHadeya/ce5793ad79b67e8845e7e6edad1a64f7 to your computer and use it in GitHub Desktop.
cs50 pset2/caesar
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, string argv[])
{
//int number = atoi(argv[1]); // convert the string argv to use it in the if condition
// checking if the user use the command line comands correctly or not
if ((argc != 2) || (atoi(argv[1]) < 0))
{
printf ("Usage: ./caesar k\n");
return 1;
}
int x = 0;
string plaintext = get_string("plain text: ");
int charactars = strlen(plaintext); // getting the number for the charactars to use it later in the for loop
//string ciphertext = plaintext;
printf("ciphertext: ");
for(int i = 0; i < charactars; i++) //loop over each charactar the user has enter to check if it's a letter or not and cipher it or leave it if it's not a letter
{
char letter = plaintext[i];
if (isalpha(letter))
{
x = letter;
x += atoi(argv[1]); // convert the string the user has provied as a key and use it in the cipher
if (isalpha(x))
{
printf("%c", x);
}
else
{
do // this loop just in case the user enter huge number I actually used goto; but I heard it's not that good so I wrote the loop
{
x -=26;
}
while (!isalpha(x));
printf("%c", x);
}
}
else
{
printf("%c",plaintext[i]);
}
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment