08-03-2010, 11:55 AM
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace rot13
{
class Program
{
static void Main(string[] args)
{
//Rot13 Cipher
//The String that is inputted is shifted 13 letters
Console.Write("Enter a String to Cypher::> ");
string str1 = Console.ReadLine();
char[] ch = str1.ToCharArray();//converts string to an array
int mycharint;
for (int i = 0; i < ch.Length; i++)
{
mycharint = (int)ch[i];//casts a character into an integer
//lower case letters for converson n-z
if ((mycharint >= 110) && (mycharint <=123))
{
mycharint -= 13;
ch[i] = (char)mycharint;//casts an integer into a character
}
//convert a-n
else if ((mycharint >= 97) && (mycharint <= 110))
{
mycharint += 13;
ch[i] = (char)mycharint;
}
//Upper case letters for conversion
else if ((mycharint >= 78)&& (mycharint <=90))
{
mycharint -= 13;
ch[i] = (char)mycharint;
}
else if((mycharint >=65)&& (mycharint <=78))
{
mycharint += 13;
ch[i] = (char)mycharint;
}
//Numbers and other characters such as []\;':
else if (((mycharint >= 32) && (mycharint <= 64)|| (mycharint >=91) && (mycharint <=96)))
{
ch[i] = (char)mycharint;
}
else if (mycharint >= 123)//all characters past z are not converted
{
ch[i] = (char)mycharint;
}
else
{
mycharint += 13;
ch[i] = (char)mycharint;
}
}
Console.Write("Cyphered Text is now::> ");
//Write Each Character one by one
for (int m = 0; m < str1.Length; m++)
{
Console.Write("{0}", ch[m]);
}
Console.WriteLine();
}
}
}