Advertisement
theelitenoob

Rotational Cipher

Jul 13th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1. // usage: rot.exe <#> <left/right> "<string>"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. int main(int argc, char *argv[]){
  7.     char* input = argv[3]; // get input
  8.     char* shift = argv[2]; // location of shift
  9.     char* num = argv[1]; // #num input
  10.     int shiftAmount = atoi(num);
  11.     if(num == NULL || input == NULL || shift == NULL){
  12.         printf("Invalid Usage!\n");
  13.         exit(8);
  14.     }
  15.     int i; // iterator
  16.     if((strcmp(shift, "left") == 0) || (strcmp(shift, "right") == 0)){
  17.         if(strcmp(shift, "right") == 0){
  18.             for(i = 0; i < strlen(input); i++){
  19.                 if(input[i] + shiftAmount <= 126)
  20.                        input[i] = input[i] + shiftAmount;
  21.                 else
  22.                        input[i] = input[i] + shiftAmount - 94;
  23.             }
  24.         }else{
  25.             for(i = 0; i < strlen(input); i++){
  26.                 if(input[i] - shiftAmount >= 32)
  27.                        input[i] = input[i] - shiftAmount;
  28.                 else
  29.                        input[i] = input[i] - shiftAmount + 94;
  30.             }
  31.         }
  32.     }else{
  33.         printf("Invalid Direction of Shift!\n");
  34.         exit(8);
  35.     }
  36.     puts(input);
  37.     // free up mem
  38.     free(&input);
  39.     free(&shift);
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement