Created
July 25, 2018 23:39
-
-
Save toshvelaga/9c670b88dc72df21bd94141e37c91147 to your computer and use it in GitHub Desktop.
CS50 Ceaser
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
/* CS50 solution. Feel free to check with any online compiler. To get credit from Harvard you may have to make a few changes. | |
hmu at s.velaga@uky.edu if you have any questions. Also I haven't learned about proper code style and formatting - | |
if you wanna hmu to tell me this looks like shit and how to make it look better pls do */ | |
#include <stdio.h> //includes fget and stdin // | |
#include <ctype.h> | |
#include <stdlib.h> | |
#include <string.h> //used for strlen // | |
int main() | |
{ | |
// Asks the user for some plaintext to cipher // | |
char plaintext[100]; | |
printf("\nPlease entUer some text that you want encrypted. . . . "); | |
fgets(plaintext,100,stdin); | |
// Asks the user to enter in a key // | |
int k; | |
printf("\nPlease enter a positive integer value (this is your key!). . . . "); | |
scanf("%d", &k); | |
// separates each character of the plaintext string | |
// turns plaintext characters into ASCII equivalents | |
// adds the key to the ASCII #'s and then turns ASCII #'s' into characters again | |
// prints out each character in the encrypted plaintext | |
int num; | |
char ASCnum; | |
for (int i = 0, n = strlen(plaintext); i < n; i++) | |
{ | |
if (plaintext[i]>= 'A' && plaintext[i] <= 'Z') | |
{ num = plaintext[i] - 'A'; | |
ASCnum = num + 65 + k % 26; | |
printf("\n%d", ASCnum); | |
} | |
else if (plaintext[i] >= 'a' && plaintext[i] <= 'z') | |
{ num = plaintext[i] - 'a'; | |
ASCnum = num + 97 + k % 26; | |
printf("\n%d", ASCnum); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment