Created
March 24, 2010 23:52
-
-
Save chucknthem/342981 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#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