loading...

re: Daily Challenge #42 - Caesar Cipher VIEW POST

FULL DISCUSSION
 

C#

Rept.it

using System;

class MainClass {
  public static void Main (string[] args) {
    var caesarCipher = new CaesarCipher();

    caesarCipher.Encrypt(3, "attack from the woods at dawn");
  }
}

public class CaesarCipher {
  public void Encrypt(int key, string text){

    if(key < 1 || key > 26){
      throw new Exception("Key must be between 1 and 26 inclusive.");
    }

    Console.WriteLine("Plain:");
    Console.WriteLine(text);

    var decText = new char[text.Length];

    for(int i = 0; i < text.Length; i++){
      int dec = (int)text[i];

      if(dec >= 65 && dec <= 90 || dec >= 97 && dec <= 122){
        decText[i] = (char)(dec + key);
      } else {
        decText[i] = (char)dec;
      }
    }

    Console.WriteLine("Encrypted:");
    Console.WriteLine(decText);
  }
}
code of conduct - report abuse