Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- int shift(char c);
- int pass = 0;
- int d = 0;
- int main(int argc, string argv[])
- {
- if (argc !=2)
- {
- printf("Usage: .vigenere key\n");
- return 1;
- }
- string key = argv[1];
- //non-ints should return non-0, so string should return 0
- if (atoi(key) != 0)
- {
- printf("Usage: .vigenere key\n");
- return 1;
- }
- if (argc == 2)
- {
- //iterates through the string length of arg 2
- for (int i = 0; i < strlen(argv[1]); i++)
- {
- //if characters in arg 2 are NOT digits, then pass gets 1 point
- if (isdigit(argv[1][i]) == 0)
- {
- //printf("%c\n", argv[1][i]);
- pass++;
- }
- //otherwise the program terminates
- else
- {
- printf("%c\n", argv[1][i]);
- printf("Usage: .vigenere keyword\n");
- return 1;
- }
- // printf("Pass: %i\n", pass);
- }
- if (pass > 0)
- {
- string text = get_string("plaintext: ");
- printf("ciphertext: ");
- for (int i = 0; i < strlen(text); i++)
- {
- int c = 0;
- //printf("strlenKey: %lu\n", strlen(key));
- //printf("d: %i\n", d);
- //char changed = text[i] + shift(key[i]);
- //printf("%c\n", changed);
- if (d >= strlen(key))
- {
- d = 0;
- }
- for (int j = 0; j <= 0; j++)
- {
- if (isalpha(text[i]) !=0 )
- {
- printf("%i\n", text[i]);
- printf("%i\n", shift(key[d]));
- char code_char = shift(key[d]) + text[i];
- printf("%i\n", code_char);
- if (code_char > 122)
- {
- code_char = (code_char) - 26;
- }
- printf("%i\n", code_char);
- printf("%c", code_char);
- c++;
- d = c + d;
- }
- else
- {
- char code_char = text[i];
- printf("%c", code_char);
- }
- //printf("key: %c\n", key[j]);
- }
- //printf("%i\n", d);
- //printf("shift value: %i\n", shift(key[i]));
- }
- printf("\n");
- }
- //if pass is more than 0, which means all characters are digits, prints success
- if (pass == 0)
- {
- printf("Usage: .vigenere keyword\n");
- return 1;
- }
- }
- return 0;
- }
- int shift(char c)
- {
- if (isalpha(c) != 0)
- {
- if (isupper(c) != 0)
- {
- int shift_by = c-65;
- return shift_by;
- }
- if (islower(c) != 0)
- {
- int shift_by = c-97;
- return shift_by;
- }
- }
- else
- {
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement