Skip to content

Instantly share code, notes, and snippets.

/caesar.c Secret

Created Apr 6, 2017
Embed
What would you like to do?
caesar.c - shared from CS50 IDE
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int c;
int k;
int p1;
int main(int argc, string argv[]) {
if (argc == 0 || argc >2) {
printf ("ERROR\n");
return 1;
}
k = atoi(argv[1]);
string p = GetString();
printf("plaintext: %s\n", p);
printf ("ciphertext: ");
for (int i = 0, n = strlen(p); i < n; i++) {
if (isalpha (p[i])) {
if (isupper (p[i])) {
p1 = p[i] - 65;
c = (p1 + k)%26 + 65;
printf ("%c", (char) c);
}
else if (islower (p[i])) {
p1 = p [i] - 97;
c = (p1 + k)%26 + 97;
printf ("%c", (char) c);
}
}
if (isdigit (p[i])) {
c = p[i];
printf ("%c", (char) c);
}
if (ispunct (p[i])) {
c = p[i];
printf ("%c", (char) c);
}
if (isblank (p[i])) {
c = p[i];
printf ("%c", (char) c);
}
}
printf ("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment