Code Red > .NET

Read file, write file

<< < (2/8) > >>

MickD:
I think the logic is there, a dictionary/map is just like a db, it has a key (like primary key if you like) which is the link to the stored data. In each loop you give the dictionary the key and it gives you access to the data.
To create the dict just use a loop and create a record for each char. The best part is if the key doesn't exist (say it's a space or comma etc.) you can just 'continue' the loop.

As far as the file read/write goes I haven't looked at C# for quite a while now so I'm no real help there I'm afraid.

Kerry:

David, sorry, I'm just flying past ..

perhaps Have a look an using LINQ ...

http://msdn2.microsoft.com/en-us/library/bb397940.aspx#Mtps_DropDownFilterText

OR regex may do the trick.

MickD:
I just remembered too, a hashtable may be easier and faster(??).
Just another option.

SomeCallMeDave:
Breaking codes?

You could use the Enumerator of a String to read each character.

I'm sure this isn't great C# coding, but it seems to work


--- Code: ---using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace CharCount
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] counts = new int[91];
            using (StreamReader sr = new StreamReader("c:/testfile.txt"))
            {
                String fullFile = sr.ReadToEnd();
                String upper = fullFile.ToUpper();

                IEnumerator chars = upper.GetEnumerator();
                while (chars.MoveNext())
                {
                    char current = (char)chars.Current;
                    int currInt = Convert.ToInt16(current);
                    if (currInt >= 48 & currInt <= 90 )
                    {
                        counts[currInt]++;
                    }
                }
            }

            for (int i = 48; i < 58; i++)
            {
                Console.WriteLine("\nNumber of {0}'s is {1}", Convert.ToChar(i), counts[i]);
            }
            for (int i = 65; i < 91; i++)
            {
                Console.WriteLine("\nNumber of {0}'s is {1}", Convert.ToChar(i), counts[i]);
            }

            Console.WriteLine("\nHit enter to continue");
            Console.ReadLine();
        }
    }
}




--- End code ---

MickD:
Nice one David! Simple too :)

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version