Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- int shift(char c);
- char cipher(int x, char y);
- int main(int argc, string argv[])
- {
- //save argv 1 to key
- string key = argv[1];
- string text;
- //rest of the code only executes if user enters a word without any numbers
- if (argc != 2) {
- printf("Usage: ./vigenere keyword\n");
- return 1;
- } else {
- for (int i = 0, j = strlen(key); i < j; i++) {
- if (!isalpha(key[i])) {
- printf("Usage: ./vigenere keyword\n");
- }
- }
- text = get_string("plaintext: ");
- }
- printf("ciphertext: ");
- for (int i = 0, j = strlen(text); i < j; i++) {
- if(isspace(text[i])) {
- printf(" ");
- }else{
- for (int a = 0, b = strlen(key); a < b; a++) {
- if (a == b) {
- a = 0;
- }
- printf("%c", cipher(shift(key[a]),text[i]));
- }
- }
- }
- }
- int shift(char c) {
- int shift = c;
- if (shift >= 65 && shift <= 90) {
- shift -= 65;
- } else {
- shift -= 97;
- }
- return shift;
- }
- char cipher(int x, char y) {
- int pc = y;
- if (islower(y)) {
- return ((((pc + x) - 97) % 26) + 97);
- } else {
- return ((((pc + x) - 65) % 26) + 65);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement