Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- int main(int argc , string argv[])
- {
- if (argc == 2 )
- {
- int key_start_over = 0 , length_of_key = strlen(argv[1]);
- string key = argv[1];
- for (int i = 0; i < length_of_key ; i ++)
- {
- while (key[i] >= '0' && key[i]<= '9') /* accept no numbers at the key */
- {
- printf("Usage: %s k\n",argv[0]);
- return 1 ;
- }
- }
- printf("plaintext:");
- string c = get_string(); /* the plaintext */
- printf("ciphertext:");
- if (c!= NULL)
- {
- for (int i2 = 0 , length_c = strlen(c) ; i2 < length_c ; i2++)
- {
- if (isupper(key[key_start_over]))
- {
- key[key_start_over] -= 'A';
- /* if the character is capital take 65 from it // ex: if char is 'A' that means character equals 0 */
- }
- if (islower(key[key_start_over]))
- {
- key[key_start_over] -= 'a';
- /* if the character is small take 97 from it // ex: if char is 'b' that means character equals 1 */
- }
- if (isalpha (c[i2]) && c[i2] != ' ') /* if the character is a letter and its not a "space" */
- {
- if (isupper(c[i2]) && c[i2] + key[key_start_over] > 'Z' )
- { /*if the character is capital and if the character + the key is bigger than Z minus 26 which means start from the beginning */
- printf("%c",((c[i2]+key[key_start_over]) - 26) );
- }
- else if(isupper(c[i2]) && (c[i2] + key[key_start_over] ) <= 'Z' )
- {
- printf("%c",(c[i2]+key[key_start_over]));
- }
- if (islower(c[i2]) && c[i2] + key[key_start_over] > 'z')
- { /*if the character is small and if the character + the key is bigger than z minus 26 which means start from the beginning */
- printf("%c",((c[i2] + key[key_start_over]) - 26));
- }
- else if (islower(c[i2]) &&( c[i2] + key[key_start_over]) <= 'z')
- {
- printf("%c",(c[i2] + key[key_start_over]));
- }
- key_start_over ++ ;
- if (key_start_over >= length_of_key)
- { /* if the key letter ends start over */
- key_start_over = 0 ;
- }
- }
- else
- { /* if the character is a space print it */
- printf("%c",c[i2]);
- }
- }
- }
- printf("\n");
- }
- else
- { // if the command-line-argument is less or bigger than 2 print this error message //
- printf("Usage: %s k\n",argv[0]);
- return 1 ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement