Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Need help with character frequency report

c# ascii visual studio text file

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1 JonErnst

JonErnst

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 05 July 2014 - 06:48 AM

Hi, 

 

I'm new to the C#  scene. Only been doing this for a few months from 0 experience.  :)

 

I am in a class right now that is on C# programming in college. I have a homework assignment I'm stuck on and need a little help figuring out how to code a couple items:

 

The assignment is to design a C# program that creates a letter frequency report from a text file and displays the character (all lowercase, uppers are changed to lowercase), ASCII value of each character, the frequency in which they appear in the file, and the percentage of said frequency from the total number of characters, and then the total number of characters at the bottom. 

 

So far, I have figured out how to display the characters, the frequency, and the total number of characters, but I'm stuck on: 1) the ASCII values and 2) the percent, and 3) sorting the list by frequency.

 

Here's my code so far:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;
using LibUtil;
using LibDate;

namespace Ch8Prb3
{
  class Program
  {
    const string INPUT_FILE_NAME = "\\CIS210\\Ch8Prb3\\TextDat.Txt";
    const string OUTPUT_FILE_NAME = "\\CIS210\\Ch8Prb3\\Ch8Prb3Rpt.Txt";

    int count;
    static StreamReader fileIn;
    static StreamWriter fileOut;

    static void Main()
    {
      ConsoleApp.ClrScr(); IdentifyApplication();
      OpenFiles(); LetterFreq();
      CloseFiles();
    }

    static void IdentifyApplication()
    {
      Console.WriteLine();
      Console.WriteLine("Application: Ch8Prb3 -- Find and display a character-frequency  ");
      Console.WriteLine("                        report of only letter chracters and their");
      Console.WriteLine("                        ASCII values from a text file.");
      Console.WriteLine();
    }
    static void OpenFiles()
    {
      try
      {
        fileIn = File.OpenText(INPUT_FILE_NAME);
        Console.WriteLine("{0} was opened", INPUT_FILE_NAME);
      }
      catch
      {
        Console.WriteLine("Error: {0} does not exist\n", INPUT_FILE_NAME);
        ConsoleApp.Exit();
      }
      try
      {
        fileOut = File.CreateText(OUTPUT_FILE_NAME);
        Console.WriteLine("{0} was created\n", OUTPUT_FILE_NAME);
      }
      catch
      {
        Console.WriteLine("Error: {0} could not be created\n", OUTPUT_FILE_NAME);
        ConsoleApp.Exit();
      }
    }
    static void LetterFreq()
    {
      int[] c = new int[(int)char.MaxValue];
      int total = 0;
      int j = 0;


      string s = File.ReadAllText("\\CIS210\\Ch8Prb3\\TextDat.Txt");
      s = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToLower(s.ToLower());
      byte[] value = Encoding.ASCII.GetBytes(s);
      double percent;

      foreach (char t in s)
      {
        c[(int)t]++;
      }
      PrintHeader();
      for (int i = 0; i < (int)char.MaxValue; i++)
      {
        if (c[i] > 0 && char.IsLetter((char)i))
        {
          total += c[i];
          percent = c[i] / total * 100;          
          fileOut.WriteLine("  {0}  {1,3}  {2,3}  {3,2:f2}", (char)i, value, c[i], percent);
        }        
      }
      fileOut.WriteLine();
      fileOut.WriteLine("Number of Characters: {0}", total);
    }   

    static void PrintHeader()
    {
      fileOut.WriteLine("   Chapter 8 Problem 3");
      fileOut.WriteLine("Character Frequency Report");
      fileOut.WriteLine("      {0:MM/dd/yyyy}", Date.Today);
      fileOut.WriteLine();
      fileOut.WriteLine("      ASCII               ");
      fileOut.WriteLine("Char  Value  Freq  Percent");
      fileOut.WriteLine("----  -----  ----  -------");

    }
    
    static void CloseFiles()
    {
      fileIn.Close(); fileOut.Close();
    }
  }
}

This program then outputs to a .txt file. I have attached it to show you what I have so far. For some reason, I keep getting "system.byte[]" instead of the ASCII value of a character, and I can't get the percentiles to figure beyond the first string!

 

Any help is appreciated!

 

Thanks!  :o

 

New Programmer

Attached Files


Edited by JonErnst, 05 July 2014 - 06:48 AM.


#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 05 July 2014 - 04:55 PM

StreamReader sr = new StreamReader (  ##yourfilename## );
 
string fileContent = sr.ReadToEnd().ToLower();
 
// FileContent has now an all-lowercase string with the file contents ;)
 


#3 JonErnst

JonErnst

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 05 July 2014 - 05:20 PM

Thanks, Black Rabbit.

 

I have that taken care of, but my main problems are:  1) the ASCII values, 2) figuring the percent, and 3) sorting the list by frequency.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download