Skip to content

Instantly share code, notes, and snippets.

@Retterath
Created July 12, 2019 16:39
Show Gist options
  • Save Retterath/04b2bee5359e0c267a3d2cf185c460e4 to your computer and use it in GitHub Desktop.
Save Retterath/04b2bee5359e0c267a3d2cf185c460e4 to your computer and use it in GitHub Desktop.
Moving characters ASCII up and down depending if the input is valid
using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConcertRegex
{
class Program
{
static void Main(string[] args)
{
string artistPattern = @"^(?<artist>[ A-z']*)$";
string songPattern = @"^(?<song>[A-Z ]*)$";
string keepPattern = @"[^ @']";
string line = string.Empty;
while ((line = Console.ReadLine()) != "end")
{
string[] tokens = line.Split(":");
string artist = tokens[0];
string song = tokens[1];
bool artistIsValid = Regex.IsMatch(artist, artistPattern);
bool songIsValid = Regex.IsMatch(song, songPattern);
if (!artistIsValid ||!songIsValid)
{
Console.WriteLine("Invalid input!");
}
else if (artistIsValid && songIsValid)
{
Match artistMatch = Regex.Match(artist, artistPattern);
Match songMatch = Regex.Match(song, songPattern);
StringBuilder sb = new StringBuilder();
int length = artist.Length;
string text = $"{artistMatch.Groups["artist"].Value}@{songMatch.Groups["song"].Value}"; // The wanted format
foreach (char symbol in text)
{
char newSymbol = symbol;
bool isValidSymbol = Regex.IsMatch(newSymbol.ToString(), keepPattern); // Check if this is valid
if (isValidSymbol)
{
newSymbol += (char)length; // The number of this result will be converted to char and moved up by the length
if (symbol <= 90 && newSymbol >= 90) // If its less or bigger than Z
{
newSymbol -= (char)26; // Change it to A
}
else if (symbol <= 122 && newSymbol >= 122) // If its less or bigger than z
{
newSymbol -= (char)26; // Change it to a
}
}
//sb += newSymbol.ToString(); cannot do this opperation, therefor append can be used
sb.Append(newSymbol);
}
Console.WriteLine($"Successful encryption: {sb}");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment