Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. #define KEY_DIVIDOR 256
  7. #define CAESAR_VALUE 1
  8. #define MAX_INPUT_LENGTH 256
  9. #define MAX_COUNTER_LENGTH 255
  10.  
  11. int main(int argc, char **argv)
  12. {
  13.   char user_input[MAX_INPUT_LENGTH];
  14.   char reverse_input[MAX_INPUT_LENGTH];
  15.   int read;
  16.   int counter = 0;
  17.   int space_counter = 0;
  18.   int key;
  19.   int key_holder = 0;
  20.   int m = 0;
  21.   int space_counter_holder = 0;
  22.  // int k = 0;
  23.   read = getchar();
  24.   while((read != EOF) && (read != '\n') && (read != '\0')) // todo: ctrl d,plain text bez njuline
  25.   {
  26.     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
  27.     {
  28.       printf("[ERR] too many characters\n");
  29.       return -1;
  30.     }
  31.     if((isupper(read)) || (!isalpha(read) && !isspace(read)))
  32.     {
  33.       printf("[ERR] invalid characters\n");
  34.       return -2;
  35.     }
  36.     else
  37.     {
  38.       if(isspace(read))
  39.       {
  40.         space_counter++;
  41.       }
  42.       user_input[counter++] = read;
  43.       read = getchar();
  44.     }
  45.   }
  46.   user_input[counter] = '\0';
  47.   if(counter-space_counter == 0)
  48.   {
  49.     printf("plain text: %s", user_input);
  50.     printf("encrypted text: \n");
  51.   }
  52.   else
  53.   {
  54.     printf("plain text: %s", user_input);
  55.     space_counter_holder = counter - space_counter;
  56.     key = KEY_DIVIDOR % (space_counter_holder);
  57.     printf("%d",key);
  58.     if(key == 0)
  59.     {  
  60.       for(int i = counter-1,k=0; i>=0; i--, k++)
  61.       {
  62.         reverse_input[k] = user_input[i];
  63.       }
  64.       reverse_input[counter] = '\0';
  65.       printf("encrypted text: %s\n", reverse_input);
  66.     }
  67.     else
  68.     {
  69.       for(m=0; m<counter; m++)
  70.       {
  71.         key_holder = user_input[m];
  72.         if(key_holder >=  'a' && key_holder <= 'z')
  73.         {
  74.           key_holder += key;
  75.           if(key_holder > 'z')
  76.           {
  77.             key_holder = key_holder - 'z' + 'a' - CAESAR_VALUE;
  78.           }
  79.           user_input[m] = key_holder;
  80.         }
  81.       }
  82.       printf("encrypted text: %s\n",user_input);
  83.     }
  84.   }
  85.   return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement