Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- [rotn Help]:
- This (not very elegant) program transposes the letters in the provided string either by the number specified in the first parameter or, if that number is 0, for all 26 values of the alphabet.
- TODO:
- Input values need to be sanitized.
- Example usage:
- rotn 13 hacker
- -> unpxre
- rotn 13 unpxre
- -> hacker
- rotn 0 hacker
- -> [1] ibdlfs
- -> ...
- -> ...
- -> [26] hacker
- */
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <stdlib.h>
- #define kAlphabet 26
- char helpString[] = "[rotn Help]:\n\nThis program transposes the letters in the provided string either by the number specified in the first parameter or, if that number is 0, for all 26 values of the alphabet.\n\nTODO:\nInput values need to be sanitized.\n\nExample usage:\n\t\trotn 13 hacker\n\t\t-> unpxre\n\t\trotn 13 unpxre\n\t\t-> hacker\n\n\t\trotn 0 hacker\n\t\t-> [1] ibdlfs\n\t\t-> ...\n\t\t-> ...\n\t\t-> [26] hacker\n\n";
- char transformWith(char c, int moveby, int upc)
- {
- int pos = 0;
- char *ults = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- char *llts = "abcdefghijklmnopqrstuvwxyz";
- char retval = c;
- int startIndex = 0;
- int i = 0;
- for(i = 0; i < kAlphabet; i++)
- {
- char test;
- if (upc > 0)
- {
- test = ults[i];
- } else
- {
- test = llts[i];
- }
- if (c == test)
- {
- startIndex = i;
- pos = startIndex + moveby;
- if (pos > 25)
- {
- pos %= 26;
- }
- if (upc > 0)
- {
- retval = ults[pos];
- } else
- {
- retval = llts[pos];
- }
- }
- }
- return retval;
- }
- void rotateStr(char str[], int rb)
- {
- int counter = 0;
- for (counter = 0; str[counter] != '\0'; counter++)
- {
- int upc = 0;
- char c = str[counter];
- if (isupper(c))
- {
- upc = 1;
- }
- printf("%c", transformWith(c, rb, upc));
- }
- }
- int main(int argc, char *argv[])
- {
- int rotate;
- if (argc == 3)
- {
- if (isdigit(*argv[1]))
- {
- int repeatTimes = 1;
- char *end;
- rotate = strtol(argv[1], &end, 10);
- if (rotate == 0)
- {
- repeatTimes = 26;
- }
- int r = 0;
- for (r = 0; r < repeatTimes; r++)
- {
- int rotateWith;
- if (repeatTimes == 1)
- {
- rotateWith = rotate;
- } else
- {
- rotateWith = r+1;
- }
- printf("[%d] ", rotateWith);
- rotateStr(argv[2], rotateWith);
- printf("\n");
- }
- }
- } else
- {
- printf("\n%s", helpString);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement