2

I am currently creating a program that uses arrays to categorize ASCII characters in a text document. I am stuck when it comes to creating the array itself, which is a critical part to the project's functionality. It is also suggested that I make the array out of charfrequency objects, which I know my code for is not quite right for this particular project. I used the code from another similar project, but am unsure how to translate it to a project that reads text from a file. I have included my charfrequency class code for reference as to a general idea of what I'm trying to do. I also need to display the results in a format like this:

H(72) = 1
e(101) = 1
l(108) = 2
o(111) = 1
.(46) = 1 

I do not understand programming very well, so detailed explanations with relatively simple terms would be very helpful.

{
public class CharFrequency
{
    private char m_character;
    private long m_count;

    public CharFrequency(char ch)
    {
        Character = ch;
        Count = 0;
    }

    public CharFrequency(char ch, long charCount)
    {
        Character = ch;
        Count = charCount;
    }

    public char Character
    {
        set
        {
            m_character = value;
        }

        get
        {
            return m_character;
        }
    }

    public long Count
    {
        get
        {
            return m_count;
        }
        set
        {
            if (value < 0)
                value = 0;

            m_count = value;
        }
    }

    public void Increment()
    {
        m_count++;

    }

    public override bool Equals(object obj)
    {
        bool equal = false;
        CharFrequency cf = new CharFrequency('\0', 0);

        cf = (CharFrequency)obj;

        if (this.Character == cf.Character)
            equal = true;

        return equal;
    }

    public override int GetHashCode()
    {
        return m_character.GetHashCode();
    }

    public override string ToString()
    {
        String s = String.Format("Character '{0}' ({1})'s frequency is {2}", m_character, (byte)m_character, m_count);

        return s;
    }

}

}

Cheeseop
  • 31
  • 1
  • 5

3 Answers3

7

Since Unicode matches ASCII codes you can just select ASCII range with Enumerable.Range:

var allAscii = Enumerable.Range('\x1', 127).ToArray();

Note that C#/.Net uses UTF-16 (C# and UTF-16 characters) to represent char, but if you only looking for ASCII range it is not a problem (as ASCII covers characters with codes 1-127 it will not conflict with surrogate pairs which are encoded into 2 char in a string).

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
1

You can simply store your character frequencies in Dictionary<char, long>.

Riad Baghbanli
  • 3,105
  • 1
  • 12
  • 20
1

Perhaps you want to look at this:

http://stackoverflow.com/questions/3665757/c-sharp-convert-char-to-int

If it were me I would create an array in which I stored the character occurances like this:

long[] charCount = new long[256];

And then each time I see a character convert it to its integer value with something like:

int idx = (int)char.GetNumericValue(c);

And then count that character occurance like:

charCount[idx]++;
Paul Coldrey
  • 1,389
  • 11
  • 20