1

I'm trying to create a process to count the frequency of each letter in a given input string. The code I have so far seems to work fine, except for updating the value of the int at a given index in the int array. When I debug the code it is successfully finding the index of the string array which corresponds to the character of the moment, so it appears that the problem is with the line: alphabetCount[index] = alphabetCount[index]++;

Here is the code.

    string[] alphabet = {"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"};
    int[] alphabetCount = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    string baseInput = string.Empty;

    private void button_count1_Click(object sender, EventArgs e)
    {
        baseInput = textBox_base.Text.ToUpper();
        int length = baseInput.Length;

        for (int i = 0; i < length; i++)
        {
            try
            {
                int index = Array.IndexOf(alphabet,baseInput[i].ToString());
                alphabetCount[index] = alphabetCount[index]++;
            }
            catch
            {
                //MessageBox.Show("nope");
            }
        }
    }
I think I can code
  • 647
  • 1
  • 6
  • 18
  • 1
    Maybe try `alphabetCount[index] = ++alphabetCount[index];`? Or the tried and trued way `alphabetCount[index] = alphabetCount[index] + 1;`. – Tim Jan 14 '15 at 20:15
  • `var result = baseInput.GroupBy(c => c).Select(g => new { Char = g.Key, Count = g.Count() }).ToList();` :) – EZI Jan 14 '15 at 20:16
  • When you use the `++` operator you don't need to _assign_ it. Simply use the `++` operator alone: `alphabetCount[index]++;`, it'll increment itself. – Pierre-Luc Pineault Jan 14 '15 at 20:21

3 Answers3

1

You can simplify the counter.

int[] alphabetCount =
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
string baseInput = string.Empty;

private void button_count1_Click(object sender, EventArgs e)
{
    baseInput = textBox_base.Text.ToUpper();

    foreach(var character in baseInput)
    {
        if(char.IsLetter(baseInput))
        {
            // A is ASCII 65, Z is ASCII 90
            // Subtract 65 to put it in the alphabetCount range
            var index = character - 65;
            alphabetCount[index]++;
        }
    }
}
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
0

The problem is alphabetCount[index]++ first assigns alphabetCount[index] and then increments the value. You need to use pre increment operator instead:

alphabetCount[index] = ++alphabetCount[index];

You can simply reproduce this behaviour like this:

var arr = new int[2];
arr[0] = arr[0]++;

Console.Write(arr[0]); // 0
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • 3
    @Tim What is the meaning of this ambiguous code `alphabetCount[index] = ++alphabetCount[index];` ? `alphabetCount[index]++;` is enough. – EZI Jan 14 '15 at 20:20
-2

You only have to write alphabetCount[index]++; or alphabetCount[index] = alphabetCount[index]+1; (equivalent)

or you use linq:

string words = "Hello World";
Dictionary<char, int> counted = words.ToUpper().GroupBy(ch => ch).OrderBy(g => g.Key).ToDictionary(g => g.Key, g => g.Count());
Mitja
  • 863
  • 5
  • 22
  • What is the meaning of `OrderBy`, since `Dictionary` doesn't guarantee the order. – EZI Jan 14 '15 at 20:23
  • Try ``OrderBy(g => g.Count())`` instead and you will see it is ordered. Where did you get the information about the non guaranteed order? – Mitja Jan 14 '15 at 20:26
  • I went with changing the line to just alphabetCount[index]++; – I think I can code Jan 14 '15 at 20:28
  • @IthinkIcancode then why did you *accept* this unrelated code? – EZI Jan 14 '15 at 20:32
  • @EZI read the first sentence. That's the answer to his problem! Don't you know where did you get the information? – Mitja Jan 14 '15 at 20:38
  • @MitjaS. then read my comment (below the question) posted 5 mins before than your answer. – EZI Jan 14 '15 at 20:41
  • @MitjaS. http://stackoverflow.com/questions/4007782/the-order-of-elements-in-dictionary http://msdn.microsoft.com/en-us/library/xfhwa508.aspx *`For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair structure representing a value and its key. The order in which the items are returned is undefined.`* – EZI Jan 16 '15 at 18:17
  • That is related to the initial use after you Add some items. If you use LINQ, the methods have effect. If I have time and I'm at home, I will write a reason why. – Mitja Jan 16 '15 at 18:31
  • @MitjaS. *"If you don't add/remove items later, you'll get them in the order you have inserted"* is your assumption,only. It may work for now, but as MSDN says, it never guarantees the order and this behavior may change with the next .Net version.Would you write a code based on some assumptions. I wouldn't. – EZI Jan 16 '15 at 22:10