Advertisement
SalvatorederRote

Caesar

Sep 2nd, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <cs50.h>
  4. #include <ctype.h>
  5. #include <string.h>
  6.  
  7.  
  8.  
  9. int main (int argc ,char* argv[])
  10. {
  11.  
  12.     if (argc != 2)
  13.     {
  14.         printf("Try again");
  15.         return 1;
  16.     }
  17.    
  18.     int key = atoi(argv[1]); /// Get key turn it into an Int
  19.    
  20.     string p = GetString();
  21.     char sum[strlen(p)]; /// Declare the string length of p as char
  22.    
  23.    
  24.     for ( int i = 0, n = strlen(p);i < n; i++) /// Iterate over the string length of p and print enciphered code
  25.    {
  26.        if isalpha(p[i])
  27.        {
  28.            if isupper(p[i])
  29.            {
  30.                sum[i] = (p[i] - 'A' + key) % 26 + ('A');
  31.            }
  32.            else if islower(p[i])
  33.            {
  34.                sum[i] = (p[i] - 'a' + key) % 26 +('a');
  35.            }
  36.            else
  37.            {
  38.                 sum[i] = p[i];
  39.            }
  40.        }
  41.    }
  42.     printf("%s\n",sum);
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement