Skip to content

Instantly share code, notes, and snippets.

@bee-san
Last active May 30, 2016 19:15
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 bee-san/142ffc56f82f8602a51c004b762d0bd4 to your computer and use it in GitHub Desktop.
Save bee-san/142ffc56f82f8602a51c004b762d0bd4 to your computer and use it in GitHub Desktop.
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
string message;
int main(int argc, string argv[]){// checks to make sure argument is correct and valid
if (argc < 2 | argc > 2)
{ printf("Not enough arguments\n"); return 1;}
string key = argv[1]; for (int i = 0, n = strlen(key); i < n; i++) {if (!isalpha(key[i])){printf("Key is not entirely aphabetical");
return 1;}}
//printf("Please enter your message here: ");
message = GetString();
int length = strlen(message);
for (int i = 0, y = 0, n = strlen(message); i < n; i++)
// the life and soul of the party
{
int KeyForLetter = tolower(key[y % length]) - 'a';
// gets the key for each indivudal letter
if (isupper(message[i]))
{
printf("%c", 'A' + (message[i] - 'A' + KeyForLetter) % 26);
// if message is upper, this keeps it al in same case
// charecter is equal to ascii of A plus the indiviudal letter added
// to the key mod 26 to reserve wrap aroudn
y++;
}
else if (islower(message[i]))
{
// for message lower
printf("%c", 'a' + (message[i] - 'a' + KeyForLetter) % 26);
y++;
}
else
{
printf("%c", message[i]);
}
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment