• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How do I write a program counted letters in C#?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write a program that creates a probabilistic model for a text file with counted occurrences of specific letters.
And it must display it in the table how many individual characters (in ASCII code) are in the text read from the file.
and the probability calculated using:
P = number of occurrences / all characters

What I have tried:

var plik = File.ReadAllLines("example.txt");
       var liczba = 'z' - 'a' + 1;
       
       var liczniki = Enumerable.Repeat(0, liczba).ToList();
       
       foreach (var słowo in plik) {
           foreach (var znak in słowo) {
               
               if (znak >= 'a' && znak <= 'z') {
                   liczniki[znak - 'a'] += 1;
               }    
           }
 
Marshal
Posts: 80508
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does that code work? Have you tested it? I have only had a very quick look at it, and I hardly know any C#, but it looks all right.
How are you calculating the total?
Are you getting 0 for all your “P”s?
 
Saloon Keeper
Posts: 5606
214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are familiar with Dictionaries,  then it is easy to make a frequency table. The total number is the sum of the values, and the probabilities are easy to calculate.
 
Campbell Ritchie
Marshal
Posts: 80508
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . Dictionaries . . . a frequency table. . . .

And there is an example very similar to that in the Java™ Tutorials.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic