Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- void increment(char*, int);
- void decrement(char*, int);
- void rotateLeft(char*, int);
- void rotateRight(char*, int);
- int main(int argc, char* argv[]){
- FILE *original = fopen(argv[1], "r");
- int length = strlen(argv[2]);
- char encrypt[length];
- strcpy(encrypt, argv[2]);
- if(NULL == original){
- printf("%s is an invalid file.\n", argv[1]);
- exit(1);
- }
- char string[100];
- fscanf(original, "%s", string);
- while(!feof(original))
- {
- int len = strlen(string);
- for(int a = 0; a < length; a++)
- {
- char key = encrypt[a];
- switch(key)
- {
- case 'L':
- rotateLeft(string, len);
- break;
- case 'R':
- rotateRight(string, len);
- break;
- case 'I':
- increment(string, len);
- break;
- case 'D':
- decrement(string, len);
- break;
- default:
- printf("Invalid encryption key \'%c\'\n", key);
- exit(1);
- }
- }
- printf("%s\n", string);
- fscanf(original, "%s", string);
- }
- return 0;
- }
- void increment(char* string,int length)
- {
- for(int a = 0;a<length;a++)
- {
- if(isdigit(string[a]) || isalpha(string[a]))
- {
- if(string[a] == 'z')
- string[a] = 'a';
- else if(string[a] == 'Z')
- string[a] = 'A';
- else if(string[a] == '9')
- string[a] = '0';
- else
- string[a] += 1;
- }
- }
- return;
- }
- void decrement(char* string, int length)
- {
- for(int a = 0;a<length;a++)
- {
- if(isdigit(string[a]) || isalpha(string[a]))
- {
- if(string[a] == 'a')
- string[a] = 'z';
- else if(string[a] == 'A')
- string[a] = 'Z';
- else if(string[a] == '0')
- string[a] = '9';
- else
- string[a] -= 1;
- }
- }
- return;
- }
- void rotateRight(char* string, int length)
- {
- char temp[length+10];
- temp[0] = string[length-1];
- for(int a=1;a<length;a++)
- {
- temp[a]=string[a-1];
- }
- strcpy(string, temp);
- return;
- }
- void rotateLeft(char* string, int length)
- {
- char temp[length+10];
- temp[length-1] = string[0];
- for(int a = 0; a < length-1; a++)
- {
- temp[a] = string[a+1];
- }
- strcpy(string, temp);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement