Skip to content

Instantly share code, notes, and snippets.

@jwieder
Last active August 29, 2015 14:10
Show Gist options
  • Save jwieder/881d6fb3590f552051db to your computer and use it in GitHub Desktop.
Save jwieder/881d6fb3590f552051db to your computer and use it in GitHub Desktop.
Simple Vigenere cipher written in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cs50.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Error code 1 Input is the Wrong Number of Arguments\n");
return 1;
}
else
{
string k = argv[1];
for(int e = 0; e < strlen(k); e++)
// second loop
// ensures each character is a letter
{
if (isalpha(k[e]))
{continue;}
if (!isalpha(k[e]))
printf("Error Code 2 Argument %s is not a letter\n", k);
return 1;
{break;}
}
//take string of plaintext and begin encoding
string plain = GetString();
int i = 0;
for (int j = 0; j < strlen(plain); j++)
{
if (isalpha(plain[j]) == 0)
{
//leave special chars alone
printf("%c", plain[j]);
i++;
}
else
{
char a = plain[j];
int x = a;
int l = ((j - i) % strlen(k));
char m = k[l];
int d = m;
if (x > 64 || x < 91)
//lower case chars
{
if (d < 91)
{
int e = d - 65;
int v = e + x;
if (v > 90)
{
int o = v - 26;
printf("%c", o);
}
else
{
printf("%c", v);
}
}
else
//upper case chars
{
int e = d - 97;
int v = e + x;
if (v > 122)
{
int o = v - 26;
printf("%c", o);
}
else
{
printf("%c", v);
}
}
}
}
}
printf("\n");
return 0;
}
}
@jwieder
Copy link
Author

jwieder commented Dec 29, 2014

this was used in harvard's CS50, and requires one of their libraries (cs50.h) to compile correctly. As time permits I will rewrite it to remove the dependency, as it is really not required. The dependencies are the function GetString() and the struct "string".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment