Click here to Skip to main content
15,917,642 members
Home / Discussions / C#
   

C#

 
GeneralRe: Character case assistance needed Pin
OriginalGriff23-Feb-10 8:47
mveOriginalGriff23-Feb-10 8:47 
GeneralRe: Character case assistance needed Pin
OriginalGriff23-Feb-10 8:46
mveOriginalGriff23-Feb-10 8:46 
AnswerRe: Character case assistance needed Pin
Luc Pattyn23-Feb-10 12:04
sitebuilderLuc Pattyn23-Feb-10 12:04 
GeneralRe: Character case assistance needed Pin
venomation23-Feb-10 22:16
venomation23-Feb-10 22:16 
GeneralRe: Character case assistance needed Pin
Luc Pattyn24-Feb-10 1:11
sitebuilderLuc Pattyn24-Feb-10 1:11 
GeneralRe: Character case assistance needed Pin
venomation24-Feb-10 4:29
venomation24-Feb-10 4:29 
GeneralRe: Character case assistance needed Pin
Luc Pattyn24-Feb-10 5:17
sitebuilderLuc Pattyn24-Feb-10 5:17 
GeneralRe: Character case assistance needed Pin
venomation24-Feb-10 7:26
venomation24-Feb-10 7:26 
Ok everyone thanks for your help it was very helpful Big Grin | :-D

Here is the final version of the code and I feel I have beaten it to death:

using System;
using System.Text;
using System.IO;
//using System.Diagnostics;

namespace CharacterReader
{
    unsafe class Program
    {
        static readonly byte[] Letter = new byte[26];//stores each letter in the alphabet
       // static Stopwatch stopWatch = new Stopwatch();
        static string _fileName;//filename to search for

        //static uint[] value = new uint[26];
        [STAThread]//allows a windos form controll to be called on an single thread
        static void Main()
        {
            using (System.Windows.Forms.OpenFileDialog open = new System.Windows.Forms.OpenFileDialog())
            {
                if (open.ShowDialog().Equals(System.Windows.Forms.DialogResult.OK))
                {

                    _fileName = open.FileName;

                }
            }


            uint* value = stackalloc uint[26];
            Load();//loads the alphabet into Letters
            Console.WriteLine("Reading File...");
           // stopWatch.Start();
            ReadFile( _fileName);
            Console.WriteLine("Analysing file...");
            AnalyseFile(ref value);
            Console.WriteLine("Done!");
            GetTotal(ref value);
            //stopWatch.Stop();
           // Console.WriteLine(stopWatch.Elapsed.TotalMilliseconds);
            Console.ReadKey();

        }
        static int _index;//used for various things throughout
        static int _i;//used for loops
        static void Load()
        {

            for (byte i = 97; i < 123; i++)
            {
                Letter[_index] = i;
                _index++;
            }
            _index = 0;
        }

        const int BufferSize = 512;//buffersize to store the files contents while reading
        static readonly StringBuilder Contents = new StringBuilder(99999);//stores the text read
        static private void ReadFile(string filename)
        {

            FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);

            StreamReader streamReader = new StreamReader(fileStream);

            char[] fileContents = new char[BufferSize];


            int* ptr = stackalloc int[1];
            *ptr = streamReader.Read(fileContents, 0, BufferSize);
            while (*ptr > 0)
            {
                Contents.Append(fileContents);
                *ptr = streamReader.Read(fileContents, 0, BufferSize);
            }
            streamReader.Close();
            fileStream.Close();


        }


        static void AnalyseFile(ref uint* value)
        {
            byte charPtr;
            for (_i = 0; _i < Contents.Length; _i++)
            {
                charPtr = (byte)Contents[_i];//gets a character from stream

                if (charPtr < 65) continue;//ignoring less than A
              
                if (charPtr > 122) continue;//ignoring less than A
                if (charPtr > 90 && charPtr < 97) continue;//ignoring less than A

                if (charPtr < 97)//if upper, shift it to 0-25
                {
                    value[charPtr - 65] += 1;
                }
                else
                {
                    value[charPtr - 97] += 1;
                }

            }
        }


        static void GetTotal(ref uint* value)
        {

            uint* biggestValue = stackalloc uint[1];


            for (_i = 0; _i < 26; _i++)
            {
                if (*biggestValue >= value[_i]) continue;
                *biggestValue = value[_i];
            }

            for (_i = 0; _i < 26; _i++)
            {
                if (!(*biggestValue).Equals(value[_i])) continue;
                _index = _i;
                break;
            }

            Console.WriteLine("The most common char is {0} with {1} hits", (char)Letter[_index], *biggestValue);
        }


    }
}


I read the entire English bible in under 6ms !
Questionrun PHP code on remote machine, from C# Pin
andyxfun23-Feb-10 4:31
andyxfun23-Feb-10 4:31 
AnswerRe: run PHP code on remote machine, from C# Pin
Dave Kreskowiak23-Feb-10 5:09
mveDave Kreskowiak23-Feb-10 5:09 
GeneralRe: run PHP code on remote machine, from C# Pin
andyxfun23-Feb-10 5:40
andyxfun23-Feb-10 5:40 
QuestionGetting Domain UserNames? Pin
Pawan Kiran23-Feb-10 3:55
Pawan Kiran23-Feb-10 3:55 
AnswerRe: Getting Domain UserNames? Pin
Dave Kreskowiak23-Feb-10 4:01
mveDave Kreskowiak23-Feb-10 4:01 
Questionrequesting the run of a .php file? Pin
andyxfun23-Feb-10 3:35
andyxfun23-Feb-10 3:35 
AnswerRe: requesting the run of a .php file? Pin
Dave Kreskowiak23-Feb-10 4:00
mveDave Kreskowiak23-Feb-10 4:00 
GeneralRe: requesting the run of a .php file? Pin
andyxfun23-Feb-10 4:15
andyxfun23-Feb-10 4:15 
GeneralRe: requesting the run of a .php file? Pin
Dave Kreskowiak23-Feb-10 5:03
mveDave Kreskowiak23-Feb-10 5:03 
GeneralRe: requesting the run of a .php file? Pin
andyxfun23-Feb-10 5:32
andyxfun23-Feb-10 5:32 
AnswerRe: requesting the run of a .php file? Pin
Luc Pattyn23-Feb-10 4:30
sitebuilderLuc Pattyn23-Feb-10 4:30 
GeneralRe: requesting the run of a .php file? Pin
andyxfun23-Feb-10 4:35
andyxfun23-Feb-10 4:35 
AnswerRe: requesting the run of a .php file? Pin
Eddy Vluggen23-Feb-10 6:37
professionalEddy Vluggen23-Feb-10 6:37 
GeneralRe: requesting the run of a .php file? Pin
andyxfun23-Feb-10 6:50
andyxfun23-Feb-10 6:50 
GeneralRe: requesting the run of a .php file? Pin
andyxfun23-Feb-10 6:54
andyxfun23-Feb-10 6:54 
AnswerRe: requesting the run of a .php file? Pin
Saksida Bojan23-Feb-10 7:13
Saksida Bojan23-Feb-10 7:13 
Questionfast write txt file Pin
AlexB4723-Feb-10 2:54
AlexB4723-Feb-10 2:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.