-1
using System;

class Decrypter 
{
static void Main ( string [] args )
{ 
    //The encrypted data is read from a file to a string, this 
    string encryptedData = System.IO.File.ReadAllText(@"C:\Users\TomTower\Desktop\Programming and Data Structures\Assessment 1\EncryptedText.txt");
    char[] alphabet = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

    // The code is supposed to take every character in an input string and left shift them back a 5 places. Y becomes T. B becomes W
    foreach(char c in encryptedData)
    {
        if (c == ' ')
            Console.Write(" ");
            else
            {
                int charPosition = 0;

                charPosition = Array.IndexOf(alphabet, c);

                charPosition = charPosition - 5;

                if (charPosition < 0)
                {    
                charPosition = charPosition + 26;
                }
                else;
                {
                Console.Write(alphabet[charPosition]);
                }
            };

The input data is as follows: YMJNS HWJIN GQJQJ LFHDT KYMJR FYMJR FYNHF QLJSN ZXLJT WLJGT TQJBN QQGJJ CUQTW JINSF KWJJU ZGQNH YFQPN SMNXM TRJHN YDTKQ NSHTQ STSYM JGNHJ SYJSF WDTKM NXGNW YMXYT UGTTQ JBFXG TWSTS YMJXJ HTSIT KSTAJ RGJWJ NLMYJ JSKNK YJJSN SYTRT IJXYK FRNQD HNWHZ RXYFS HJXYM JXTST KFXMT JRFPJ WXYTU QFWLJ QDXJQ KYFZL MYMJB JSYTS YTGJH TRJTS JTKYM JBTWQ I’XKN SJXYR FYMJR FYNHN FSXBM TXJBT WPSTB KTWRX YMJGF XNXTK HTRUZ YJWXH NJSHJ FSIJQ JHYWT SNHHN WHZNY WDXYT UMJQF NIYMJ KTZSI FYNTS XTKYM JINLN YFQJW FFSIN XBNIJ QDWJH TLSNX JIFXY MJKTW JKFYM JWTKY MJINL NYFQF LJXYT UGTTQ JFSFQ LJGWF NXSTB FKZSI FRJSY FQFXU JHYTK RTIJW SRFYM JRFYN HXFSI TAJWY MJQFX YHJSY ZWDBF XZXJI YTKTW RYMJY MJTWJ YNHFQ KTZSI FYNTS TKRTI JWSHT RUZYJ WXHNJ SHJJS I

and my output looks like this: http://puu.sh/layx0/27e50b70a9.png

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tom
  • 1
  • using the same code, I don't get the extra U's... – DrewJordan Nov 05 '15 at 19:22
  • Same as @DrewJordan but some things are odd in the code : `else;` and `};` (and on a side note you can use a string for alphabet it supports IndexOf and indexing like a char array see [here](https://dotnetfiddle.net/mxq5y7)) – Sehnsucht Nov 05 '15 at 19:28

1 Answers1

1

If the text in your file has non matching data in it your Array.IndexOf will return a -1. When you then adjust by adding 26 to it the output is a 'U'.

Safeguard for bad data:

    foreach (char c in encryptedData)
    {
        if (c == ' ')
        {
            Console.Write(" ");
        }
        else
        {
            int charPosition = 0;

            charPosition = Array.IndexOf(alphabet, c);

            if (charPosition == -1)
            {//Check for non expected items
                Console.Write("*");
            }
            else
            {
                charPosition = charPosition - 5;

                if (charPosition < 0)
                {
                    charPosition = charPosition + 26;
                }
                Console.Write(alphabet[charPosition]);
            }
        }
    }
Brian from state farm
  • 2,825
  • 12
  • 17
  • Maybe not sure without the original file – Brian from state farm Nov 05 '15 at 19:32
  • I'm 100% sure it's CRLF now. looking at the output, `UU` occurs after every 4 'blocks' (once for CR and once for LF). – DrewJordan Nov 05 '15 at 19:37
  • Could be simpler to replace the if/else by first doing the IndexOf and if it's -1 just print the initial char as-is (or some default char) ; that would take care of the space case or any char not in alphabet – Sehnsucht Nov 05 '15 at 19:40
  • So i could add a line of code after "if(charposition == -1)" with Console.Write("") to just make it write nothing? – Tom Nov 05 '15 at 20:00