Skip to content

Instantly share code, notes, and snippets.

@barrucadu
Forked from anonymous/Cypher.c
Created November 2, 2011 13:43
Show Gist options
  • Save barrucadu/1333652 to your computer and use it in GitHub Desktop.
Save barrucadu/1333652 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#define MAXSTR 50
char pt[MAXSTR], ct[MAXSTR], dt[MAXSTR] ;
int trans(void){ /*Transposition Cypher
*Swaps adjacent pairs of letters
*/
int i = 0;
while (pt[i] != '\0' && pt[i+1] != '\0') /* while the 2 characters are not line terminators */
{
ct[i] = pt [i+1]; /*swap the 2 characters*/
ct[i+1] = pt [i];
i = i+2; /*move on to next 2 characters*/
}
if (pt[i] == '\0') /*even number of characters*/
{
ct[i] = pt[i]; /*copy over line terminator*/
}
else
{
ct[i] = pt[i]; /*copy over last character*/
ct[i+1] = pt[i+1]; /*copy over line terminator*/
}
printf("encyphered text:\n%s\n", ct);
return 0;
}
int rot13(void){
int i = 0;
while (pt[i] != '\0')
{
switch (pt[i]){
case 'A' : ct[i] = 'N'; break;
case 'B' : ct[i] = 'O'; break;
case 'C' : ct[i] = 'P'; break;
case 'D' : ct[i] = 'Q'; break;
case 'E' : ct[i] = 'R'; break;
case 'F' : ct[i] = 'S'; break;
case 'G' : ct[i] = 'T'; break;
case 'H' : ct[i] = 'U'; break;
case 'I' : ct[i] = 'V'; break;
case 'J' : ct[i] = 'W'; break;
case 'K' : ct[i] = 'X'; break;
case 'L' : ct[i] = 'Y'; break;
case 'M' : ct[i] = 'Z'; break;
case 'N' : ct[i] = 'A'; break;
case 'O' : ct[i] = 'B'; break;
case 'P' : ct[i] = 'C'; break;
case 'Q' : ct[i] = 'D'; break;
case 'R' : ct[i] = 'E'; break;
case 'S' : ct[i] = 'F'; break;
case 'T' : ct[i] = 'G'; break;
case 'U' : ct[i] = 'H'; break;
case 'V' : ct[i] = 'I'; break;
case 'W' : ct[i] = 'J'; break;
case 'X' : ct[i] = 'K'; break;
case 'Y' : ct[i] = 'L'; break;
case 'Z' : ct[i] = 'M'; break;
case 'a' : ct[i] = 'n'; break;
case 'b' : ct[i] = 'o'; break;
case 'c' : ct[i] = 'p'; break;
case 'd' : ct[i] = 'q'; break;
case 'e' : ct[i] = 'r'; break;
case 'f' : ct[i] = 's'; break;
case 'g' : ct[i] = 't'; break;
case 'h' : ct[i] = 'u'; break;
case 'i' : ct[i] = 'v'; break;
case 'j' : ct[i] = 'w'; break;
case 'k' : ct[i] = 'x'; break;
case 'l' : ct[i] = 'y'; break;
case 'm' : ct[i] = 'z'; break;
case 'n' : ct[i] = 'a'; break;
case 'o' : ct[i] = 'b'; break;
case 'p' : ct[i] = 'c'; break;
case 'q' : ct[i] = 'd'; break;
case 'r' : ct[i] = 'e'; break;
case 's' : ct[i] = 'f'; break;
case 't' : ct[i] = 'g'; break;
case 'u' : ct[i] = 'h'; break;
case 'v' : ct[i] = 'i'; break;
case 'w' : ct[i] = 'j'; break;
case 'x' : ct[i] = 'k'; break;
case 'y' : ct[i] = 'l'; break;
case 'z' : ct[i] = 'm'; break;
default : ct[i] = pt[i];
}
i = i + 1;
}
ct[i] = pt[i]; /*copy over the line terminator*/
printf("encyphered text:\n%s\n", ct);
return 0;
}
int main(void){
int X;
printf("enter a string less than 50 characters>");
scanf("%s", &pt);
printf("0: Exit\n1: Transposition cypher\n2: ROT13\nEnter number > ");
scanf("%d", &X);
while (X != 0)
{
switch (X){
case 1 : trans(); break;
case 2 : rot13(); break;
}
printf("0: Exit\n1: Transposition cypher\n2: ROT13\nEnter number > ");
scanf("%d", &X);
strncpy (pt, ct, MAXSTR);
printf("%s\n%s\n", pt, ct);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment