Skip to content

Instantly share code, notes, and snippets.

Created December 8, 2017 13:28
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 anonymous/3b51071fe9d7226e9bdb43675290956a to your computer and use it in GitHub Desktop.
Save anonymous/3b51071fe9d7226e9bdb43675290956a to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char* argv[]){
if (argc != 2){ //check if the user is starting the program properly
printf("Usage: ./caesar k\n");
return 1;}
else{
printf("plaintext: ");
string s = get_string();//collect the plaintext from the user
int n=strlen(s);//stringlenght of the input
int k = atoi(argv[1]);//convert a the caracter inputed from the user in a int
for (int i=0; i<n; i++){
int upper= (isupper(s[i])); //case sensitivity
int punct=ispunct(s[i]);//check punctuation
if (upper !=0 ){ //branch for uppercase
int res_caps = ((int)s[i] - 64 + k);
int b_caps = (res_caps % 26) + 64; //math to get the alfabetical index
printf("%c", b_caps);}
else if (s[i] == ' '){ //branch for spaces
printf (" ");}
else if (punct != 0){ //branch for punctuation
printf("%c", s[i]);}
else{//branch for lowercase
int res = ((int)s[i] - 96 + k); //math to get the alfabetical index
int b = (res % 26) + 96;
printf("%c", b);}
}
printf("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment