Created
October 28, 2019 21:53
-
-
Save robgmerrill/f0d67a3466e8fe191fd6fdea6ff8d1c1 to your computer and use it in GitHub Desktop.
Caesar Cipher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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