Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main(int argc, string argv[])
- {
- string keyString;
- if (argc == 2)
- {
- keyString = argv[1];
- if (!isalpha(keyString[0]))
- {
- return 1;
- }
- }
- else
- {
- return 1;
- }
- int keyLength = strlen(keyString);
- string pt = get_string("Enter text: ");
- char ct[strlen(pt) + 2];
- char chaesarChar (char alpha, char key);
- int keyIndex = 0;
- for (int i = 0, n = strlen(pt); i < n; i++)
- {
- //index
- int index = keyIndex % keyLength;
- //taken, plaintext is a word
- if (isalpha(pt[i]))
- {
- if (isupper(pt[i]))
- {
- ct[i] = chaesarChar(pt[i], keyString[index]);
- if (ct[i] > 90)
- {
- ct[i] -= 26;
- }
- }
- else if (islower(pt[i]))
- {
- ct[i] = chaesarChar(pt[i], keyString[index]);
- if (ct[i] > 122)
- {
- ct[i] -= 26;
- }
- }
- }
- else
- {
- ct[i] = pt[i];
- }
- keyIndex += 1;
- ct[strlen(pt) + 1] = '\0';
- printf("%s\n", ct);
- return 0;
- }
- char chaesarChar (char alpha, char key)
- {
- char x = key;
- if (islower(key)){
- x = key - 97;
- }
- else if (isupper(key))
- {
- x = key - 'A';
- }
- char res = alpha + x;
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement