Design a program that reads an ASCII text file (provided) one byte at a time and
ID: 3745551 • Letter: D
Question
Design a program that reads an ASCII text file (provided) one byte at a time and produces an output file that contains the frequency count of how many times each character appears in the input file. Do not sort the output. A character frequency class instance must be used to represent each unique character in the file (see below) For this exercise, the character frequency objects must be processed and stored using an array. For example, given the sample input file: Hello Would yield the following output file H(72) e(11) 1 1(108) 2 o (111) 1 The output file must follow the format presented above. Character(ASCII Value) (tab) Frequency. Note that formatting anomalies are expected due to the nature of the characters themselves (e.g. the tab character, carriage return, etc.) and wil not cause a point deduction. The program should be able to be executed from the command line using the following syntax programname.exe inFile>Explanation / Answer
Please find the code below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ASCII
{
class CharacterFrequency
{
private char ch;
private int frequency;
public char Ch
{
get { return ch; }
set { ch = value; }
}
public int Frequency
{
get { return frequency; }
set { frequency = value; }
}
}
class Program
{
public string InputFileName="";
public string OutputFileName="Output.txt";
public string FilePath="";
public static SortedDictionary<char, ulong> Count(string stringToCount)
{
SortedDictionary<char, ulong> characterCount = new SortedDictionary<char, ulong>();
foreach (var character in stringToCount)
{
if (!characterCount.ContainsKey(character)) // added character to dictionary if only character is absent in charactercount
{
characterCount.Add(character, 1);
}
else
{
characterCount[character]++; // increemetned count
}
}
return characterCount;
}
static void Main(string[] args)
{
Program p = new Program();
CharacterFrequency obj = new CharacterFrequency() ;
StreamWriter streamWriter = new StreamWriter(p.OutputFileName);
try
{
p.InputFileName = args[0]; // get input
p.OutputFileName = args[1]; // get output
p.FilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\" + p.InputFileName; // get file path
if (File.Exists(p.FilePath)) // checked if file exist
{
string data = File.ReadAllText(p.FilePath); // read the data in string
var count = Program.Count(data); // passthe string to count method
foreach (var character in count)
{
streamWriter.WriteLine(character.Key+"("+(int)character.Key+")"+" "+character.Value); // write data to output file
}
streamWriter.Close();
Console.ReadLine();
}
else
{
Console.WriteLine("Please provide input File"); // if input file absent sent message to user to place input file
}
}
catch (Exception ex)
{
Console.WriteLine("Exception occured " + ex.Message.ToString());
}
}
}
}
Please like my answer .
Thank you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.