using System.Text;
namespace scream
{
///
/// Implements the Scream Cipher, encoding messages using diacritics on the letter 'A'.
///
class Program
{
///
/// The base character 'A' used for encoding messages with diacritics.
///
private const char BASE_CHARACTER = 'A';
///
/// The starting ASCII value of printable characters.
///
private const int ASCII_START = 0x20;
///
/// The ending ASCII value of printable characters.
///
///
/// Not used, but included for reference
///
private const int ASCII_END = 0x7E;
///
/// The starting Unicode code point for diacritic marks.
///
private const int DIACRITIC_START = 0x300;
///
/// The ending Unicode code point for diacritic marks.
///
///
/// Not used, but included for reference
///
private const int DIACRITIC_END = 0x36F; // Not needed, but here for reference.
/// Main entry-point for this application.
/// The component parts of the message.
static void Main(string[] args)
{
if (args[0].Equals("-q") || args[0].Equals("--quiet"))
{
QuietDown(args);
}
else if (args.Length > 0)
{
var scream = Scream(args);
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.WriteLine(scream);
}
else
{
return;
}
}
/// Quiets the given message as defined by the string arry.
/// The component parts of the message.
/// A string representing the quiet message.
private static string QuietDown(string[] messageComponents)
{
var concat = string.Concat(messageComponents.Skip(1));
StringBuilder sb = new StringBuilder();
foreach (var c in concat)
{
var index = GetAsciiIndex(c);
var ascii = GetAscii(index);
sb.Append(ascii);
}
return sb.ToString();
}
/// Screams the given message as defined by the string array.
/// The component parts of the message.
/// A string representing the screamed message.
private static string Scream(string[] messageComponents)
{
var concat = string.Concat(messageComponents);
StringBuilder sb = new StringBuilder();
foreach (var c in concat)
{
var index = GetDiacriticIndex(c);
var diacritic = GetDiacritic(index);
sb.Append(BASE_CHARACTER);
sb.Append(diacritic);
}
return sb.ToString();
}
/// Gets the zero-based index of the diacritic character.
/// The diacritic character to index.
/// The diacritic character index.
private static int GetDiacriticIndex(char character)
{
var index = character - ASCII_START;
return index;
}
/// Gets the zero-based index of the viewable ASCII character.
/// The ASCII character to index.
/// The ASCII character index.
private static int GetAsciiIndex(char character)
{
var index = character - DIACRITIC_START;
return index;
}
/// Gets a diacritic character given an index offset.
/// Zero-based index of the Diacritic character.
/// The diacritic character.
private static char GetDiacritic(int index)
{
var diacritic = (char)(DIACRITIC_START + index);
return diacritic;
}
/// Gets an ASCII character given an index offset.
/// Zero-based index of the ASCII character.
/// The ASCII character.
private static char GetAscii(int index)
{
var character = (char)(ASCII_START + index);
return character;
}
}
}