0

Hi,
I want to get a count of each english letter in a text file.The lower case and upper case letter should be taken as 1 letter(i.e A and a should be counted as 1 letter).This is my code but it takes capital A ans simple a as 2 letters. Can anyone help?

int[] c = new int[(int)char.MaxValue];
                string file = openFileDialog.FileName;
               
                    string s = File.ReadAllText(file);
                  
                    foreach (char t in s)
                    {
                        c[(int)t]++;
                    }
                    for (int i = 0; i < (int)char.MaxValue; i++)
                    {
                        if (c[i] > 0 &&
                            char.IsLetter((char)i))
                        {
                            Console.WriteLine("Letter: {0}  Frequency: {1}",
                                (char)i,
                                c[i]);
                        }
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by ddanbe
0

Line 1 : You don't have to cast char.MaxValue to an int.
Besides that you can index your string s directly., but before you do that use the ToUpper or ToLower methods of your string object.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.