Skip to content

Instantly share code, notes, and snippets.

@chucknthem
Created March 24, 2010 23:52
Show Gist options
  • Save chucknthem/342981 to your computer and use it in GitHub Desktop.
Save chucknthem/342981 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <malloc.h>
char * rot(const char *origStr, int shift) {
char *newStr = (char*)malloc(strlen(origStr) + 1);
int i = 0;
while(origStr[i]) {
if('a' <= origStr[i] && origStr[i] <= 'z') {
newStr[i] = (origStr[i] - 'a' + shift)%26 + 'a';
} else if('A' <= origStr[i] && origStr[i] <= 'Z') {
newStr[i] = (origStr[i] - 'A' + shift)%26 + 'A';
} else {
newStr[i] = origStr[i];
}
i++;
}
newStr[i] = '\0';
return newStr;
}
int main(int argc, char**argv) {
int shift = 13;
size_t nbytes = 80;
char *str = (char *) malloc (nbytes + 1);
if(argc > 1) {
int i;
for(i = 1; i < argc; i++) {
char *rotStr = rot(argv[i], shift);
printf("%s\n", rotStr);
free(rotStr);
}
} else {
int bytes_read = 0;
do {
bytes_read = getline (&str, &nbytes, stdin);
char *rotStr = rot(str, shift);
printf("%s", rotStr);
free(rotStr);
} while (bytes_read > 0);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment