Skip to content

Instantly share code, notes, and snippets.

@robgmerrill
Created October 28, 2019 21:53
Show Gist options
  • Save robgmerrill/f0d67a3466e8fe191fd6fdea6ff8d1c1 to your computer and use it in GitHub Desktop.
Save robgmerrill/f0d67a3466e8fe191fd6fdea6ff8d1c1 to your computer and use it in GitHub Desktop.
Caesar Cipher
using System;
namespace CaesarCipher
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter your secret message: ");
string input = Console.ReadLine();
char[] secretMessage = input.ToCharArray();
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'};
char[] encodedMessage = 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) % alphabet.Length;
char letterEncoded = alphabet[newLetterPosition];
encodedMessage[i] = letterEncoded;
}
string encodedString = String.Join("", encodedMessage);
Console.WriteLine($"Your encoded message is: {encodedString}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment