0

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.

1
  • You can use StringBuilder(for better performance) with capacity(userInput / 2) for the evenchar and capacity(userInput / 2 + 1) for odd char variables in the encrypting part. Also encodedInput = evenChar + oddChar; should be after the foreach cycle. Commented Nov 12, 2016 at 19:27

1 Answer 1

3

Don't loop over each character of the encoded input as you will end up processing each character twice. You are already counting up and down the string with the len and len2 variables so if you replace the foreach with:

while (len > len2)

this will only process each character of the string once

You will have to do some special casing when the string is an odd number of characters to deal with the middle character - i.e. when len and len2 are equal. To this end add the following:

            if (len == len2)
                break;

in middle of the loop so that it becomes:

        while (len > len2)
        {
            lastChar = encodedInput[len - 1];
            decodedInput.Append(lastChar.ToString());
            len--;

            if (len == len2)
                break;

            firstChar = encodedInput[len2];
            len2++;
            decodedInput.Append(firstChar.ToString());
        }
Sign up to request clarification or add additional context in comments.

1 Comment

You're amazing :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.