您的位置:首页 > 职场人生

黑马程序员 C#统计一行字符串中各个字母出现的频率(不考虑大小写,不考虑其他字符)

2012-04-15 18:44 344 查看
---------------------- android培训java培训、期待与您交流! -----------------------

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Security;

using System.Collections;

using System.Text.RegularExpressions;

namespace jiayou

{

    class Program

    {

        static int   Asc(string a)

        {

            int ch = (int)ASCIIEncoding.ASCII.GetBytes(a)[0];

            return ch;

        }

        /*public static int Asc(string character)  //  字符转换成ASSIC

        {

            if (character.Length == 1)

            {

                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();

                int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];

                return (intAsciiCode);

            }

           else

           {

              throw new Exception("Character is not valid.");

          }

        }*/

      /*  public static string Chr(int asciiCode)    //ASSIC转换成字符

        {

            if (asciiCode >= 0 && asciiCode <= 255)

            {

                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();

                byte byteArray = new byte { (byte)asciiCode };

                string strCharacter = asciiEncoding.GetString(byteArray);

                return (strCharacter);

            }

            else

            {

                throw new Exception("ASCII Code is not valid.");

            }

        } */

        static void Main(string[] args)

        {

            while (true)

            {

                Console.WriteLine("请输入想要求的字符串:");

                string str = Console.ReadLine();

                int[] a = new int[200];

                int c;

                for (int i = 0; i < str.Length; i++)

                {

                    string str2;

                    string str1 = str.Substring(i, 1);

                    if ((64 < Asc(str1) && Asc(str1) < 91) || (96 < Asc(str1) && Asc(str1) < 123))

                    {

                        str2 = str1.ToLower();

                        c = Asc(str2);

                        a[c]++;

                    }

                }

                string b;

                byte[] x;

                for (int i = 0; i < 200; i++)

                {

                    if (a[i] != 0)

                    {

                        x = BitConverter.GetBytes(i);

                        b = Encoding.ASCII.GetString(x);

                        Console.WriteLine("{0}  {1} ", b, a[i]);

                    }

                }

              

            }

        }

    }

}
 

--------------------- android培训java培训、期待与您交流! ----------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐