Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created Mar 15, 2020
Embed
What would you like to do?
Codecademy export
using System;
namespace CaesarCipher
{
class Program
{
static void Main(string[] args)
{
char[] alphabet = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
Console.WriteLine("What would your message be?");
string message = Console.ReadLine();
Console.WriteLine("Would you like to code or decode the message?");
string answer1 = Console.ReadLine();
if (answer1 == "Code" || answer1 == "code") {
char[] secretMessage = message.ToCharArray();
char[] encryptedMessage = new char[secretMessage.Length];
for (int i = 0; i < secretMessage.Length; i++) {
char letter = secretMessage[i];
int letterPosition = Array.IndexOf(alphabet, letter);
int newLetterPosition = letterPosition + 3;
char letterEncoded = alphabet[newLetterPosition];
encryptedMessage[i] = letterEncoded;
}
string end_message = String.Join("", encryptedMessage);
Console.WriteLine(end_message);
} else {
char[] secretMessage = message.ToCharArray();
char[] encryptedMessage = new char[secretMessage.Length];
for (int i = 0; i < secretMessage.Length; i++) {
char letter = secretMessage[i];
int letterPosition = Array.IndexOf(alphabet, letter);
int newLetterPosition = letterPosition - 3;
char letterEncoded = alphabet[newLetterPosition];
encryptedMessage[i] = letterEncoded;
}
string end_message = String.Join("", encryptedMessage);
Console.WriteLine(end_message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment