黑马程序员 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培训、期待与您交流! ----------------------
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培训、期待与您交流! ----------------------
相关文章推荐
- 编写一个函数,由实参传来一个字符串,统计此字符串中字母,数字,空格,和其他字符的个数,在主函数中输入字符串以及输出上述统计的结果。再考虑将算的的结果放在一个数组中
- 统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
- 统计字符串里面数字,字母,其他字符的出现次数
- 输入一行字符串分别统计出字母。数字。空格和其他字符 *利用while循环语句条件输入的字符为"\n"
- 长为10000的字符串,由a-z及A-Z组成,统计出其中出现频率最高的字母
- 编写一个程序统计输入字符串中: 各个数字、空白字符、以及其他所有字符出现的次数
- 输入一行字符,分别统计出其中的英文字母、空格、数字、和其他字符的个数
- 统计输入字符各个字母出现频率的解题思路
- 统计字符串中字母,数字,空格以及其他字符的个数。
- 输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数。
- 统计字符串中出现频率最高的字符
- 华为机试: 计算字符串的相似度、输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数
- 统计一个字符串中数字、字母、空格和其他字符的个数
- 6.2输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
- 代码:统计一行字符中英文字母、空格、数字、其他的个数(%[^\n])
- 输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数
- 编写一个程序统计输入字符串中: 各个数字、空白字符、以及其他所有字符出现的次数
- 输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数。。。
- 输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数【while循环】
- 《任意输入一个合理大小的字符串,实现逆序输出并统计一个字符串中字母、数字、空格及其他字符的个数》