Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. /*
  7. bool all_alpha(const char *str){
  8.  
  9.     char c;
  10.  
  11.     while ((c = *str++) != '\0')
  12.         if (!isalpha(c))
  13.             return false;
  14.     return true;
  15. }*/
  16.  
  17. int main(int argc, string argv[]){
  18.  
  19.     string key;
  20.     int textLength;
  21.     string text;
  22.     int shift;
  23.     char symb;
  24.  
  25.  
  26.     //if user did not provide an argument
  27.     if (argc == 1 || argc > 2){
  28.         return 1;
  29.  
  30.  
  31.     //if argv[1] provided - get a key
  32.     } else {
  33.         key = (argv[1]);
  34.     }
  35.    
  36.     //get text for encoding
  37.     text = get_string("Enter text: ");
  38.     textLength = strlen(text);
  39.  
  40.     printf("ciphertext: ");
  41.  
  42.     //filtering loop
  43.     int j = 0;
  44.     for (int i = 0; i < textLength; i++){
  45.  
  46.         if (islower[key[j]])
  47.             shift = key[j] - 'a'
  48.         else
  49.             shift = key[j] - 'A'
  50.  
  51.        
  52.         if (isaplha(text[i])) {
  53.             if (islower(text[i]))
  54.                 symb = ((((text[i] - 'a') + shift) % 26) + 'a');
  55.             else
  56.                 symb = ((((text[i] - 'A') + shift) % 26) + 'A');
  57.            
  58.             printf("%c", symb);
  59.             j++;
  60.         } else
  61.             printf("%c", text[i]);
  62.  
  63.         if (j == (strlen(key)))
  64.             j=0;
  65.     }
  66.  
  67.     printf("\n");
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement