Click here to Skip to main content
16,016,022 members
Home / Discussions / C#
   

C#

 
GeneralRe: Character case assistance needed Pin
venomation23-Feb-10 7:29
venomation23-Feb-10 7:29 
AnswerRe: Character case assistance needed Pin
harold aptroot23-Feb-10 7:33
harold aptroot23-Feb-10 7:33 
GeneralRe: Character case assistance needed Pin
venomation23-Feb-10 8:19
venomation23-Feb-10 8:19 
GeneralRe: Character case assistance needed Pin
PIEBALDconsult23-Feb-10 8:46
mvePIEBALDconsult23-Feb-10 8:46 
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 
I have decided to post the full source of the code so far, the code is a bit messy but I felt I had to break some coding standards to achieve higher performance so here it goes:

using System;
using System.Text;
using System.IO;

namespace CharacterReader
{
    unsafe class Program
    {
        static readonly char[] Letter = new char[26];//stores each letter in the alphabet
    
        static string _fileName = "lol.txt";//filename to search for


        [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
            Console.WriteLine("Reading File...");
            ReadFile(_fileName);
            Console.WriteLine("Analysing file...");
            AnalyseFile(value);
            Console.WriteLine("Done!");
            GetTotal(value);
            Console.ReadKey();

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

            for (_i = 65; _i < 91; _i++)
            {
                Letter[_index] = (char)_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(uint* value)
        {


            char* charPtr = stackalloc char[1];
            for (_i = 0; _i < Contents.Length; _i++)
            {
                for (int k = 0; k < Letter.Length; k++)
                {

                    *charPtr = Contents[_i];

                    if (*charPtr < 65 ) continue;
                    if (*charPtr >= 97 && *charPtr <= 122)//char is lower case
                    {
                        *charPtr = char.ToUpper(*charPtr);
                    }

                    if (!(*charPtr).Equals(Letter[k])) continue;
                    (value[k]) += 1;
                    break;
                }
            }
        }


        static void GetTotal(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", Letter[_index], *biggestValue);
        }


    }
}


Has anyone got some tips to make it run faster, it seems the "AnalyseFile" method seems to be slower than the file input OMG | :OMG:
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 
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 

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.