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>
- #define KEY_DIVIDOR 256
- #define CAESAR_VALUE 1
- #define MAX_INPUT_LENGTH 256
- #define MAX_COUNTER_LENGTH 255
- int main(int argc, char **argv)
- {
- char user_input[MAX_INPUT_LENGTH];
- char reverse_input[MAX_INPUT_LENGTH];
- int read;
- int counter = 0;
- int space_counter = 0;
- int key;
- int key_holder = 0;
- int m = 0;
- int space_counter_holder = 0;
- // int k = 0;
- read = getchar();
- while((read != EOF) && (read != '\n') && (read != '\0')) // todo: ctrl d,plain text bez njuline
- {
- if(counter > MAX_COUNTER_LENGTH) // TODO: handle if both cases appear, that its longer than 254(ask nikola for length) and something from the below , this errror should be printed first
- {
- printf("[ERR] too many characters\n");
- return -1;
- }
- if((isupper(read)) || (!isalpha(read) && !isspace(read)))
- {
- printf("[ERR] invalid characters\n");
- return -2;
- }
- else
- {
- if(isspace(read))
- {
- space_counter++;
- }
- user_input[counter++] = read;
- read = getchar();
- }
- }
- user_input[counter] = '\0';
- if(counter-space_counter == 0)
- {
- printf("plain text: %s", user_input);
- printf("encrypted text: \n");
- }
- else
- {
- printf("plain text: %s", user_input);
- space_counter_holder = counter - space_counter;
- key = KEY_DIVIDOR % (space_counter_holder);
- printf("%d",key);
- if(key == 0)
- {
- for(int i = counter-1,k=0; i>=0; i--, k++)
- {
- reverse_input[k] = user_input[i];
- }
- reverse_input[counter] = '\0';
- printf("encrypted text: %s\n", reverse_input);
- }
- else
- {
- for(m=0; m<counter; m++)
- {
- key_holder = user_input[m];
- if(key_holder >= 'a' && key_holder <= 'z')
- {
- key_holder += key;
- if(key_holder > 'z')
- {
- key_holder = key_holder - 'z' + 'a' - CAESAR_VALUE;
- }
- user_input[m] = key_holder;
- }
- }
- printf("encrypted text: %s\n",user_input);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement