Skip to content

Instantly share code, notes, and snippets.

@christianscott
Created May 25, 2023 03:49
Show Gist options
  • Save christianscott/14fbfd855f0b71e398d3107db7b15b5a to your computer and use it in GitHub Desktop.
Save christianscott/14fbfd855f0b71e398d3107db7b15b5a to your computer and use it in GitHub Desktop.
caesar cypher .c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc <= 1) {
printf("Usage: %s [+/-shift]\n", argv[0]);
return 1;
}
char* shift_str = argv[1];
int sign = 1;
if (shift_str[0] == '-') {
sign = -1;
shift_str++;
} else if (shift_str[0] == '+') {
shift_str++;
}
int shift = atoi(shift_str);
shift *= sign;
FILE* stdin = fopen("/dev/stdin", "r");
while (!feof(stdin)) {
char c = fgetc(stdin);
if (c == EOF) {
break;
}
if (c == '\n') {
printf("\n");
continue;
}
printf("%c", (c+shift)%256);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment