Member Avatar for chipbu

Hi guys,

I need some help to create an array with character indexing like this:
A B C D E ....
A 0 0 0 0 0
B 0 0 0 0 0
C 0 0 0 0 0
D 0 0 0 0 0
E 0 0 0 0 0
.
.

so that I can record the occurrence of a particular pair of letters. I tried to search on msdn but found no clue yet.
my guess is that I can use an alphabet enum to assign index to each letter. But the problem is that I cant use alphabet letter as textual data (ie string, char)
Can anybody give me a hint?

Recommended Answers

All 8 Replies

I don't think any one gets what you are asking, could you please restate your question? What I get from it is you are trying to find a pair of two letters inside an char array? Or maybe how many times a pair of two letters occurs?

Do you mean something like:

char a, b;
//read in a and b and change to caps
int[,] occurrences = new int[26, 26];
Console.WriteLine("The Co-occurrence of " + a + " and " + b + " is " + occurrences[a - 'A', b - 'A']);

>need some help to create an array with character indexing

Think about Indexer. Create a new type and add indexer method.

class Test
{
    static void Main()
    {
        TArray a = new TArray(3, 4);

        a['a', 'a'] = 10;
        Console.WriteLine(a['a', 'a']);
    }
}

public class TArray
{
    int[,] ar;

    public TArray(int firstbound, int secondbound)
    {
        ar = new int[firstbound, secondbound];
    }
    public int this[char  i,char j]
    {
        get
        {
            int i1 = char.ToUpper(i) - 65;
            int j1 = char.ToUpper(j) - 65;

            return ar[i1, j1];
        }
        set
        {
            int i1 = char.ToUpper(i) - 65;
            int j1 = char.ToUpper(j) - 65;
            ar[i1, j1] = value;
        }
    }
}
Member Avatar for chipbu

here is my idea, I created a Dictionary variable to hold keys (alphabet letters) and values according to each letters (eg A has value 0, B has value 1, and so on).

private int[,] digraphMatrix = new int[26, 26];   //this is the matrix that I want to record the occurrences of pairs of letters
        private Dictionary<char, int> alphabetList = new Dictionary<char, int>();                     //a list to hold value for each alphabet letters
static readonly char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 
                                               'F', 'G', 'H', 'I', 'K',
                                               'L', 'M', 'N', 'O', 'P', 
                                               'Q', 'R', 'S', 'T', 'U',
                                               'V', 'W', 'X', 'Y', 'Z'}; 
private string data;                       //the string that I want to record the occurrences

public Digraph(string path)
        {
            Read(path);
            Build();
        }
        
        private void Read(string path)      //read in the string from a file
        {
            StreamReader sr = new StreamReader(path);
            data = sr.ReadToEnd();
            data = Preprocessor.Prepare(data);     //method  from another class to filter out non-alphabetical characters
            data = Preprocessor.Pad(data);           //method from another class to pad strings to the left
            sr.Close();   
        }
        
        private void fillAlphaList()              //fill alphabetList with keys and corresponding values
        {
            for (int i = 0; i < alphabet.Length; i++)
                alphabetList.Add(alphabet[i], i);
        }

        private void Build()                   //method to fill up the matrix
        {
            char fc, sc;                           //1st and 2nd character
            int fcX, scY;                          //position of the 1st and 2nd character in the alphabet
            fillAlphaList();
            for (int i = 0; i < data.Length; i += 2)
            {
                fc = data[i];
                sc = data[i + 1];
                fcX = alphabetList[fc];
                scY = alphabetList[sc];
                digraphMatrix[fcX, scY] += 1;     //if the pair of characters occur then value in the corresponding position will increase by 1
            }
        }
Member Avatar for chipbu

I've got some problem with reading a large file, say an electronic of Lord of the Rings for example. The program just doesnt compile at all.

Member Avatar for chipbu

>need some help to create an array with character indexing

Think about Indexer. Create a new type and add indexer method.

class Test
{
    static void Main()
    {
        TArray a = new TArray(3, 4);

        a['a', 'a'] = 10;
        Console.WriteLine(a['a', 'a']);
    }
}

public class TArray
{
    int[,] ar;

    public TArray(int firstbound, int secondbound)
    {
        ar = new int[firstbound, secondbound];
    }
    public int this[char  i,char j]
    {
        get
        {
            int i1 = char.ToUpper(i) - 65;
            int j1 = char.ToUpper(j) - 65;

            return ar[i1, j1];
        }
        set
        {
            int i1 = char.ToUpper(i) - 65;
            int j1 = char.ToUpper(j) - 65;
            ar[i1, j1] = value;
        }
    }
}

1 question: why do you take away 65? Sorry for asking this but I am just a beginner programmer

1 question: why do you take away 65? Sorry for asking this but I am just a beginner programmer

The ASCII code (see: http://web.cs.mun.ca/~michael/c/ascii-table.html) for uppercase A is 65. In order to translate your char into the appropriate index number you need to subtract that. So if your character is 'B' which has the code 66 and you subtract 'A' you'll get 1, for 'C'-'A' you'll get 2, etc.

I didn't quite get what you were asking about LOTR in the post before your last one.

1 question: why do you take away 65? Sorry for asking this but I am just a beginner programmer

A character field is just a couple (or four) of bytes of information. You can implicitly cast this Char field to int, which is what adapost did. ASCII is a coding standard that defines a single byte that can be used for almost all characters in the English language and is the standard format used when international characters aren't in use.
If you cast "A" to int, it will be 65. Every letter from A to Z are incrementally represented in alphabetical order from 65 to 90 in the ASCII format. You subtract 65, you get an alphabetical indexed location from 0 to 25 for the 26 letters.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.