none
calculating the number of Special Characters occurrence in a text file. RRS feed

  • Question

  •  am facing a problem in counting the Special Characters occurrences in a text file. the <g class="gr_ gr_60 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="60" id="60">snerio</g> is " load a file in the textbox in WPF and instead of showing the file content, show the number of Special Characters in the textbox after calculation.and save this result in <g class="gr_ gr_160 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" data-gr-id="160" id="160">text</g> file. the result must look like below"

    Number of .( full stop) :232

     

    Number of -( dash) :232

    and many others. 

    Saturday, January 27, 2018 4:28 PM

All replies

  • Here is something to try, replace items var with information read in from the text file.

    string items = "ÝABCÓDE?ÃFGHÃI?YJKLÓƒƒMÆNOPRSŠTU??VZݽ¥¥¥¥";
    
    Encoding iso = Encoding.GetEncoding("ISO-8859-1");
    Encoding utf8 = Encoding.UTF7;
    byte[] utfBytes = utf8.GetBytes(items);
    byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes);
    string itemsTranslated = iso.GetString(isoBytes);
    
    var characterGroup = (
        from chr in itemsTranslated.ToCharArray()
        group chr by chr into grp
        select new
        {
            Letter = grp.Key,
            Occurrences = grp.Count(),
            Code = Convert.ToInt32((int)grp.Key)
        })
        .ToList()
        .OrderBy((item) => item.Letter.ToString());
    
    var results = (from item in characterGroup select item);
    
    
    foreach (var item in results)
    {
        Console.WriteLine($"{item.Letter}, {item.Code}, {item.Occurrences}");
    }

    Results of console writeline

    , 4, 1
    , 14, 1
    , 15, 2
    , 20, 1
    ?, 63, 4
    ¥, 165, 4
    ½, 189, 1
    A, 65, 1
    Ã, 195, 2
    Æ, 198, 1
    B, 66, 1
    C, 67, 1
    D, 68, 1
    E, 69, 1
    f, 102, 2
    F, 70, 1
    G, 71, 1
    H, 72, 1
    I, 73, 1
    J, 74, 1
    K, 75, 1
    L, 76, 1
    M, 77, 1
    N, 78, 1
    O, 79, 1
    Ó, 211, 2
    P, 80, 1
    R, 82, 1
    S, 83, 2
    T, 84, 1
    U, 85, 1
    V, 86, 1
    Y, 89, 1
    Ý, 221, 2
    Z, 90, 1
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, January 27, 2018 5:07 PM
    Moderator
  • Hello hafiz,

    Or you could try the below code.

     public static void  Main(string[] args)
            {
                string Text = "Comprehensive API reference for working with Microsoft tools, services, and technologies. Whether you're building apps, developing websites, or working with the cloud, you'll find detailed syntax, code snippets, and best practices.";
                Dictionary<char, int> di = new Dictionary<char, int>();
    
                var chars=Text.ToCharArray();
    
                for (int i = 0; i < chars.Length; i++) {
                    if (di.Keys.Contains(chars[i]))
                        di[chars[i]]++;
                    else
                        di.Add(chars[i], 1);
                }
    
                foreach (var keyvalue in di) {
                    Console.WriteLine($"the char {keyvalue.Key} occurs {keyvalue.Value} times");
                }
                Console.ReadLine();
            }

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, January 28, 2018 12:13 PM
    Moderator
  • thanks for your help . its good but unfortunately <g class="gr_ gr_100 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="100" id="100">its</g> not according to my expectations or <g class="gr_ gr_155 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="155" id="155">i</g> think <g class="gr_ gr_156 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="156" id="156">i</g> did not <g class="gr_ gr_153 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" data-gr-id="153" id="153">explained</g> my problem <g class="gr_ gr_191 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="191" id="191">properly .</g>
    Tuesday, January 30, 2018 2:56 PM
  • Hello Hafiz,

    >>its good but unfortunately not according to my expectations or  think did not explained my problem properly.

    The above example will display all characters and occur times, If you just want to calculate special characters, you just need to add some "if" logic in it.

       static void Main(string[] args)
            {
                //the list will contain special chars
                List<char> chs = new List<char> { ' ', ',', ' ' };
    
                //The input text
                string Text = "Comprehensive API reference for working with Microsoft tools, services, and technologies. Whether you're building apps, developing websites, or working with the cloud, you'll find detailed syntax, code snippets, and best practices.";
    
                //The dictionary will recode char and occur times
                Dictionary<char, int> di = new Dictionary<char, int>();
    
                //Convert the input text to char array
                var chars = Text.ToCharArray();
    
                //lope the input text
                for (int i = 0; i < chars.Length; i++)
                {
                    //if the current char is special character, will recode char and times
                    if (chs.Contains(chars[i]))
                    {
                        //if the curren char has already been adding to dictionary object, The times add 1
                        if (di.Keys.Contains(chars[i])) di[chars[i]]++;
    
                        //if the current char doesn't add to dictionary object, then add the char to dictionary object whose default time is 1
                           else di.Add(chars[i], 1);
                    }
                }
    
                //loop the dictionary object
                foreach (var keyvalue in di)
                {
                    Console.WriteLine($"the char {keyvalue.Key} occurs {keyvalue.Value} times");
                }
                Console.ReadLine();
            }

    If you have any issues, please feel free to contact me.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 31, 2018 10:45 AM
    Moderator