Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <cs50.h>
- #include <string.h>
- int main(int argc, string argv[])
- {
- if(argc==2)
- {
- for(int j=0;j<strlen(argv[1]);j++)
- {
- if(argv[1][j]>='0' && argv[1][j]<='9')
- {
- //printf("%c\n", argv[1][j]);
- }
- else
- {
- printf("Only type one key.\n");
- return 1;
- }
- }
- int k = strtol(argv[1], NULL, 10);
- //printf("%i\n", k);
- if (k>0 && k<27)
- {
- //printf("good key.\n");
- }
- else if (k>26)
- {
- do
- {
- k = k - 26;
- }
- while(k>26);
- //printf("key is actually %i\n", k);
- }
- else
- {
- printf("key must be positive.\n");
- return 1;
- }
- string plain=get_string("plaintext: ");
- printf("ciphertext: ");
- for(int i=0;i<=strlen(plain);i++)
- {
- if (plain[i] >= 'a' && plain[i] <= 'z')
- {
- int cipher= (int)plain[i] + k;
- if (cipher>122)
- {
- cipher = cipher - 26;
- }
- char c=(char)cipher;
- printf("%c",c);
- }
- else if (plain[i] >= 'A' && plain[i] <= 'Z')
- {
- int cipher= (int)plain[i] + k;
- if (cipher>90)
- {
- cipher = cipher - 26;
- }
- char c=(char)cipher;
- printf("%c",c);
- }
- else
- {
- int cipher= plain[i];
- char c=(char)cipher;
- printf("%c",c);
- }
- }
- printf("\n");
- return 1;
- }
- else
- {
- printf("Usage: ./caesar key\n");
- return 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement