Skip to content

Instantly share code, notes, and snippets.

@drmcarvalho
Last active March 9, 2018 13:11
Show Gist options
  • Save drmcarvalho/7eaf18ccbdec5f5b46c6088ee4ff7eee to your computer and use it in GitHub Desktop.
Save drmcarvalho/7eaf18ccbdec5f5b46c6088ee4ff7eee to your computer and use it in GitHub Desktop.
Cifra de Cesar.
//https://dotnetfiddle.net/8JRJZk
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using static System.Console;
public class CifraCesar
{
public static string Cifrar(string texto)
{
Regex simbolos = new Regex(@"\W|_");
if (string.IsNullOrWhiteSpace(texto)) return null;
char[] normal = new char[26] {
'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[] cifrado = new char[26] {
'D', 'I', 'N', 'S', 'X',
'E', 'J', 'O', 'T', 'Y',
'F', 'K', 'P', 'U', 'Z',
'G', 'L', 'Q', 'V', 'A',
'H', 'M', 'R', 'W', 'B',
'C'
};
List<char> saida = new List<char>();
for (int i = 0; i < texto.Length; i++)
{
if (simbolos.Matches(texto[i].ToString()).Count == 0)
{
int index = Array.IndexOf(normal, Char.ToUpper(texto[i]));
char letra = cifrado[index];
saida.Add(letra);
}
else saida.Add(texto[i]);
}
return new string(saida.ToArray());
}
}
public class Program
{
public static void Main()
{
string v = @"Era uma vez o Joaozinho. Ele foi na padaria e perguntou pro padeiro: tem pao?, e o padeiro respondeu: nao. Entao, o Joaozinho retrucou: e paes?, logo, o padeiro respondeu: naes. O Joaozinho perguntou se o pao ja havia saido, apos a resposta positiva do padeiro, o Joaozinho perguntou que horas ele voltava. Humor do bem fatec dois mil e dezoito. Diga nao as drogas.";
Write(CifraCesar.Cifrar(v));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment