Last active
May 30, 2016 19:15
-
-
Save bee-san/142ffc56f82f8602a51c004b762d0bd4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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