My project: Basically, I've written a small encrypting program in the workshop which takes user input and checks whether the character position in the loop is even, if so it will at the front of the string, else at the end. It looks something like this;
string userInput = "", encodedInput = "", evenChar = "", oddChar = "";
int charCount = 0;
Console.WriteLine("Input your text: ");
userInput = Console.ReadLine();
foreach(char character in userInput)
{
charCount++;
if ((charCount % 2) == 0)
{
evenChar = evenChar + character;
}
else
{
oddChar = character + oddChar;
}
encodedInput = evenChar + oddChar;
}
Console.WriteLine(encodedInput);
Now this works fine, when i type in "hi my name is jeff!" I get "im aei ef!fjs mny h".
Now I'm trying to write a deciphering loop. The method I chose for deciphering is basically taking the last character from the string adding it to a new empty string and then taking the first character from the string and also adding it to the same empty string and then simply decrements the overall length of the encrypted string and increments the position of the first character.
char lastChar = ' ';
char firstChar = ' ';
StringBuilder decodedInput = new StringBuilder();
int len = encodedInput.Length;
int len2 = 0;
foreach(char character in encodedInput)
{
lastChar = encodedInput[len - 1];
decodedInput.Append(lastChar.ToString());
len--;
firstChar = encodedInput[len2];
len2++;
decodedInput.Append(firstChar.ToString());
}
Console.WriteLine(decodedInput.ToString());
Now this works fine for the most part. It takes the same "im aei ef!fjs mny h" and outputs "hi my name is jeff!!ffej si eman ym ih". It mirrors the string because for each loop i produce to characters so "hi my name is jeff" turns into 36 characters. I've tried halving the loop, but you still get some mirroring.
I'm well aware that there are better or easier methods for deciphering this, but I want to do it this way for the educational purposes.
Kind regards,
Vocaloidas.