Skip to content

Instantly share code, notes, and snippets.

@henkman
Last active January 2, 2016 12:59
Show Gist options
  • Save henkman/8306987 to your computer and use it in GitHub Desktop.
Save henkman/8306987 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
static char *ALPHABET = "abcdefghijklmnopqrstuvwxyz";
static void rotate(char **string, int r)
{
char *s = *string;
int i, c;
r %= 26;
for(i = 0; i < strlen(s); i++) {
c = s[i] | 0x20;
if('a' <= c && c <= 'z') {
s[i] = ALPHABET[((int)(('z' - 'a' + 1) + (c - 'a')) + r) % 26];
}
}
}
int main(int argc, char **argv)
{
int c;
char *_string = NULL;
char *_rot = NULL;
while((c = getopt(argc, argv, "s:r:")) != -1) {
switch(c) {
case 's':
_string = optarg;
break;
case 'r':
_rot = optarg;
break;
default:
printf("Usage: caesar -s string -r rotation\n");
return EXIT_FAILURE;
}
}
if(_string == NULL || _rot == NULL) {
printf("Usage: caesar -s string -r rotation\n");
return EXIT_FAILURE;
}
rotate(&_string, atoi(_rot));
printf("%s\n", _string);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment