Koren Leslie Cohen

  • About
  • Blog
  • Contact

8 comments

C, Harvard CS50

Vigenere Cipher in C

April 2, 2014 by Koren Leslie Cohen

Takes user input and encrypts it with an alphabetical keyword.

/**
 * vigenere.c
 *
 * Koren Leslie Cohen
 *
 * Takes user input and encrypts it.
 *
 */

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

// program to encrypt user string by keyword entered in command line
int main(int argc, string argv[])
{
    // declare variables
    int shift;
    int KeyValue;

    // only accept two command line arguments
    if (argc != 2)
    {
        printf("No key or incorrect key. Run program and enter single alphabetical keyword in command line.\n");
        return 1;
    }

    // capture the second command line argument as a string
    string key = argv[1]; 

    // iterate through the key to make sure it's alphabetical
    for (int n = 0, keylength = strlen(argv[1]); n < keylength; n++)
    {
        if ((key[n] >= '0') && (key[n] <= '9'))
        {
            printf("Sorry! You were supposed to enter a keyword with only letters.  Boohoo.\n");
            return 1;
        }
    }

    // get the plain text
    // doesn't pass check50 when printed - printf("Type a word to encrypt!\n");
    string PlainText = GetString();   

    // encrypt - iterate over the characters in string, printing each one encrypted
    for(int i = 0, j = 0, length = strlen(PlainText); i < length; i++, j++)
    {
        // start the key again if key shorter than PlainText
        if (j >= strlen(key))
        {
            j = 0;
        }

        // loop was "breaking" when I used "key[j]" for "A" or "a" due to the above
        // declared a variable of a different name to make the program run properly
        KeyValue = key[j]; 

        // skip key[j] if PlainText[i] is not an alpha character
        if (!isalpha(PlainText[i]))
        {
            j = (j - 1);
        }  

        // makes Aa = 0, Zz = 25 for the uppercase letters 
        if ((KeyValue >= 'A') && (KeyValue <=  'Z'))
        {
            KeyValue = (KeyValue -  'A');
        }

        // makes Aa = 0, Zz = 25 for the lowercase letters
        if ((KeyValue >= 'a') && (KeyValue <= 'z'))
        {
            KeyValue = (KeyValue - 'a');
        }

        // encryption
        shift = (PlainText[i] + KeyValue);

        // wrapping after Z for uppercase letters
        if (isupper(PlainText[i]) && (shift > 'Z'))
        {
            shift = (shift - 26);
        }

        // wrapping after z for lowercase letters
        if (islower(PlainText[i]) && (shift > 'z'))
        {
            shift = (shift - 26);
        }

        // test - printf("The ASCII value of %c is %d.\n\n", shift, shift);
        // test - printf("In calculating %c + %d...\n", PlainText[i], key[j]); 
        // test - printf("The value of PlainText %c is %d\n", PlainText[i], PlainText[i]);
        // test - printf("The value of key is %d\n", KeyValue); 

        // if character is alphabetical, print encrypted result
        if (isalpha(PlainText[i]))
        {
            printf("%c", shift);
        }

       // if non-alphabetical character, print as is   
        else
        {
            printf("%c", PlainText[i]);
        }

    }  
    printf("\n");
    return 0;
}

Result:

vigenere.c

  • About
  • Latest Posts
Connect
Koren Leslie Cohen
Product manager at Facebook. Former senior product manager at Dollar Shave Club in Los Angeles and software engineer at J.Crew / Madewell in New York City. Recovering trial lawyer.
Connect
Latest posts by Koren Leslie Cohen (see all)
  • PM Career Story - April 28, 2022
  • How to Transition into Product Management - December 26, 2017
  • What I’ve Learned in My First Few Months as a Product Manager - October 14, 2015

Related Posts

FizzBuzz in Ruby (and the Strangeness of Ruby Loops)
Caesar Cipher in C

Share

Facebook Google+ Twitter Pinterest Email

Comments Cancel reply

Your email address will not be published. Required fields are marked *

*

code

  1. Naveen says

    December 7, 2015 at 7:36 am

    Hey, I have been following code solutions for CS50. They are precise and explained great.

    Reply
    • Koren Leslie Cohen says

      December 7, 2015 at 4:51 pm

      Happy to help!

      Reply
      • Gus says

        February 24, 2017 at 9:53 pm

        Leslie, Naveen,
        please disregard my previous request about the cs50 library.
        If my gcc (in my Mac) complains is because I have to download that library first.
        Thank you both,
        Cordially,
        G.

        Reply
    • Gus says

      February 24, 2017 at 9:46 pm

      Hello Naveen,
      would you please clarify how you’ve used CS50?
      Thank you for your time,
      G
      P.S. if “commented out”, the gcc compiler delivers a lot of errors.
      ……………………………………………………………………………………………….
      Hello Leslie,
      if Naveen does not see muy request for help,
      would you please explain what kind of library is that (i.e. CS50)?
      When “included” (i.e. #include ), my compiler (gcc 4.2.1, x86_64) “says” that it cannot find it

      Reply
  2. jide says

    March 30, 2016 at 12:35 pm

    thanks. this was helpful to me

    Reply
  3. kevin says

    September 27, 2016 at 5:28 pm

    Thanks a bunch!

    Reply
  4. kevin says

    September 27, 2016 at 5:30 pm

    Life Saver!

    Reply
  5. Megan says

    March 20, 2018 at 10:19 am

    BLESS YOU, YOU KIND HUMAN. I’ve been struggling with how to make the key wrap around for almost a week now. It always makes so much sense when you see the answer. Thank you SO much.

    Reply

Back to Blog

  • GitHub
  • Instagram
  • LinkedIn
  • RSS
  • Twitter

Looking for something?

Copyright 2023 Koren Leslie Cohen