C#C
C#4y ago
badb0nny

❔ trying to encode and decode a user inputted string

Im struggling to figure out why it doesnt decode back into the original string?
using System;

namespace Encode_Decode_Program
{
    class Globals
    {
        public static string encodeDecodeKey;
        public static int encodeInteger;
        public static string encodedString;
        public static string decodedString;
    }

    class Program
    {
        static void Main()
        {
            Random rnd = new Random();
            Console.WriteLine("Enter a string which you want decoded: "); string encodeString = Console.ReadLine();

            for (int x = 0; x < encodeString.Length; x++)
            {
                int asciiValue = encodeString[x];
                Globals.encodeInteger = rnd.Next(1, 9);
                Globals.encodeDecodeKey = Globals.encodeDecodeKey + Globals.encodeInteger;
                char encodedCharacter = (char)(asciiValue + Globals.encodeInteger);
                Console.WriteLine(Globals.encodeDecodeKey);
                Console.WriteLine(encodedCharacter);
                Globals.encodedString = Globals.encodedString + encodedCharacter;
            }

            for (int x = 0; x < Globals.encodedString.Length; x++)
            {
                int asciiValue = Globals.encodedString[x];
                char decodedCharacter = (char)(asciiValue - Globals.encodeDecodeKey[x]);
                Console.WriteLine(decodedCharacter);
                Globals.decodedString = Globals.decodedString + decodedCharacter;
            }

            Console.WriteLine("Encoded string: " + Globals.encodedString);
            Console.WriteLine("Decoded string: " + Globals.decodedString);
            Console.ReadLine();
        }
    }
}

I know its not the neatest code of all time im still trying to get into the habit of writing neat code
Was this page helpful?