1,105,447 Community Members

Frequency Analysis program problem

Member Avatar
Youg
Newbie Poster
16 posts since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I think it's just ging to be something stupid that I've missed since the program itself runs, though doesn't solve the decrypted text. I've included the text from the decrypted file below the code and any help would be great.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Frequency_Analysis
{
    class Program
    {
        static void Main(string[] args)
        {
            //array to hold the english language letters
            char[] aLetter = { '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' };

            //variable that holds the amount of words in the text
            int wCount = 0;
            //Read the task_2 text file into lines array
            string[] lines = System.IO.File.ReadAllLines(@"task_2.txt");

            foreach (string line in lines)
            {
                //creates a new string builder for the string 'line'
                StringBuilder b = new StringBuilder(line);  

                int i = 0;
                //Prints the encrypted text to console screen
                Console.WriteLine("Encrypted text");
                Console.Write("\n" + line);
                Console.WriteLine("\n");

                //initializes a new instance of the KeyValuePair structure with the specified key and value
                foreach (KeyValuePair<char, int> letter in GetLetterCounts(line).OrderByDescending(letter => letter.Value))
                
                {
                    //Character checker 
                    if (char.IsLetterOrDigit(letter.Key))   
                    {
                        //prints out the letter and how many times its occured
                        Console.WriteLine("Letter " + letter.Key + " : " + letter.Value);
                        //replaces the current letter with the english letter equivalent
                        b.Replace(letter.Key, aLetter[i]);
 
                        i++;
                    }

                    //checks for spacing
                    else if (letter.Key == ' ')
                    {
                        //puts the values of the letter into word count
                        wCount = letter.Value;   
                    }
                }

                //prints out the amount of words to the console
                Console.Write("\nTotal Words: " + wCount);    
                Console.WriteLine("\n");      
                //prints the string builders to console
                Console.WriteLine(b + "\n");

            }

            //keep window open til user is finished
            Console.WriteLine("Press any key to finish");
            System.Console.ReadKey();
        }

        //create dictionary function
        static Dictionary<char, int> GetLetterCounts(string myString)   
        {
            //Contains char and intager
            Dictionary<char, int> letter = new Dictionary<char, int>(); 
            //loop to check each character inside the string
            foreach (char c in myString)

                //checks to see if character is within
                if (letter.ContainsKey(c))
                {
                    //increment myDict character by one
                    letter[c]++;    
                }

                else
                {
                    //makes the characters have a count of one, if its sent to 0 it undercounts the letters and words
                    letter[c] = 1;  
                }

            //Returns the value
            return letter;
        }
    }
}

BERMN GWNDN BUUBANOG WZNARTIVUWZHD PBD B QZTJAN RQ NMZKNOGIV UZHGRAZBI ZOGNOG, GWRJTW ZGD ZLUANDDZROZDGZH NSNHJGZRO QRAEBKN B MNAV HINBA ZKNB RQ ZGD OBGJAN. ZG DNNLNK GR EN B DRAG RQ LRODGNA, RA DVLERI ANUANDNOGZOT B LRODGNA, RQ B
QRAL PWZHW ROIV B KZDNBDNK QBOHV HRJIK HROHNZMN. ZQ Z DBV GWBG LV DRLNPWBG NSGABMBTBOG ZLBTZOBGZRO VZNIKNK DZLJIGBONRJD UZHGJAND RQ BO RHGRUJD, B KABTRO, BOK B WJLBO HBAZHBGJAN, Z DWBII ORG EN JOQBZGWQJI GR GWN DUZAZG RQ GWN GWZOT. B UJIUV, GNOGBHINK WNBK DJALRJOGNK B TARGNDXJN BOK DHBIV ERKV PZGW AJKZLNOGBAV PZOTD, EJG ZG PBD GWN
TNONABI RJGIZON RQ GWN PWRIN PWZHW LBKN ZG LRDG DWRHFZOTIV QAZTWGQJI.

Member Avatar
phorce
Veteran Poster
1,016 posts since Jul 2011
Reputation Points: 119 [?]
Q&As Helped to Solve: 133 [?]
Skill Endorsements: 29 [?]
Featured
 
0
 

Hey,

You have not included the frequency analysis itself - Just the alphabet array.

For example:

char[] aLetter = { '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' };

Needs to have the frequencies (1.4, 3.2, 2.2) etc and then ordered by the most frequent.

Hope this helps

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

To what are you comparing it to decrypt it?

Member Avatar
Youg
Newbie Poster
16 posts since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

To what are you comparing it to decrypt it?

how do you mean?

Member Avatar
Youg
Newbie Poster
16 posts since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Do you mean reorganise the letter array into the most frequent? I've re done it to that now of:

char[] aLetter ={ 'e', 't', 'a', 'i', 'o', 'n', 's', 'r', 'h', 'u', 'l', 'c',
  'd', 'f', 'm', 'y', 'g', 'p', 'w', 'b', 'v', 'x', 'q', 'k', 'j', 'z' };
Member Avatar
phorce
Veteran Poster
1,016 posts since Jul 2011
Reputation Points: 119 [?]
Q&As Helped to Solve: 133 [?]
Skill Endorsements: 29 [?]
Featured
 
0
 

Do you mean reorganise the letter array into the most frequent? I've re done it to that now of:

char[] aLetter ={ 'e', 't', 'a', 'i', 'o', 'n', 's', 'r', 'h', 'u', 'l', 'c',
  'd', 'f', 'm', 'y', 'g', 'p', 'w', 'b', 'v', 'x', 'q', 'k', 'j', 'z' };

In essence, the way you've done it is "Hardcoded" and they should be sorted by means of like LINQ and not just a pre-defined array (This will make you pass your assignment, however, is not really a good way to program)

Your array should contain something like:

A = 8.2, B = 2.3 etc.. And then sorted that way.

Doing the method you've done, have you tried to match the the two values together? For example:

Alphabet: E T A I O N S R H U I C D F M Y G P W B V X Q K J Z
Ciphertext: C Y X S M P E T U V Q L I H K W J A D O R Z B G N

?

Member Avatar
Youg
Newbie Poster
16 posts since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 


Your array should contain something like:

A = 8.2, B = 2.3 etc.. And then sorted that way.

Doing the method you've done, have you tried to match the the two values together? For example:

Alphabet: E T A I O N S R H U I C D F M Y G P W B V X Q K J Z
Ciphertext: C Y X S M P E T U V Q L I H K W J A D O R Z B G N

?

Thats what was going for, what would be your suggestion then? Going with the frequency the letter appears in the document or in the english alphabet for the array.

Member Avatar
phorce
Veteran Poster
1,016 posts since Jul 2011
Reputation Points: 119 [?]
Q&As Helped to Solve: 133 [?]
Skill Endorsements: 29 [?]
Featured
 
0
 

I would personally go with the one you've gone for.. I.e:

char[] aLetter ={ 'e', 't', 'a', 'i', 'o', 'n', 's', 'r', 'h', 'u', 'l', 'c', 'd', 'f', 'm', 'y', 'g', 'p', 'w', 'b', 'v', 'x', 'q', 'k', 'j', 'z' };

Do the text analysis on the text file.

I think Thines01 can implement a way of switching the letters =)

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
1
 

@phorce is Right.

Also, it might be easier if your first action on the data is to read it as an array (or List) of char rather than strings, since the decode will be done one char at a time.

Also, in your loop, you check to see if a character is a letter or digit or space. What if it is punctuation? Do you skip it or print it?

Member Avatar
Youg
Newbie Poster
16 posts since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

punctuation is printed to console screen, I've managed to get it working now by editing the text file into one line instead of multiple, though I would still like to learn. will try changing the string load to char

Member Avatar
phorce
Veteran Poster
1,016 posts since Jul 2011
Reputation Points: 119 [?]
Q&As Helped to Solve: 133 [?]
Skill Endorsements: 29 [?]
Featured
 
1
 

Does the text completely decrypt now?

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

This page has a decrypter program on it you can use for testing. It won't help with the code, but it's interesting to see.

Member Avatar
Youg
Newbie Poster
16 posts since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

yeah the text has completely decrypted and readable. And thanks for all the help!

Member Avatar
phorce
Veteran Poster
1,016 posts since Jul 2011
Reputation Points: 119 [?]
Q&As Helped to Solve: 133 [?]
Skill Endorsements: 29 [?]
Featured
 
0
 

No problem :) If this thread is solved, please mark it and give reputation points to those who helped!

Question Answered as of 1 Year Ago by phorce and thines01
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: