读书人

求 字符串中相同字符的个数。该如何解

发布时间: 2012-02-06 15:52:45 作者: rapoo

求 字符串中相同字符的个数。。。。。
假设随便输入字符串 adfdfkeaaaldcleidlfadtuduu
判断出 有多少个a 多少个d 等等
在C#中怎么写?

[解决办法]
string s = "adfdfkeaaaldcleidlfadtuduu";

Dictionary <char,int> dic = new Dictionary <char,int>();
for(int i=0;i<s.length;i++)
{
if(dic.Contains(s[i]))
{
dic[s[i]]= dic[s[i]]+1;
}
else
{
dic.Add(s[i],1);
}
}

[解决办法]
不会LINQ 好象LINQ一行就可以了

给你个循环的把


private void button2_Click(object sender, EventArgs e)
{
Dictionary<char, int> _Value = GetTextCount("adfdfkeaaaldcleidlfadtuduu");
}

public Dictionary<char, int> GetTextCount(string p_Text)
{
Dictionary<char, int> _Re = new Dictionary<char, int>();

while (true)
{
int _Value =p_Text.Length;
char _CharKey = p_Text[0];
p_Text = p_Text.Replace(_CharKey.ToString(), "");
_Re.Add(_CharKey, _Value - p_Text.Length);
if (p_Text.Length == 0) return _Re;
}
}
[解决办法]
思路:
1.取字符串长度1,
2.取第一个字符x,
3.将字串中的x全部替换成空,将值赋给原串
4.再取字串长度2
5.两个长度之差就是x的个数
6.循环,直到字串为空.
[解决办法]
修改了string s = "adfdfkeaaaldcleidlfadtuduu";

Dictionary <char,int> dic = new Dictionary <char,int>();
for(int i=0;i <s.length;i++)
{
if(dic.ContainsKey(s[i]))
{
dic[s[i]]= dic[s[i]]+1;
}
else
{
dic.Add(s[i],1);
}
}

[解决办法]
正则表达式。。判断匹配次数。。。汗
[解决办法]

[解决办法]

C# code
 string str = "adfdfkeaaaldcleidlfadtuduu ";            Regex re = new Regex(@"a");            MatchCollection mac = re.Matches(str);             int a =0;            foreach (Match m in mac)            {               a = Regex.Matches(str, m.Value).Count;                //Console.WriteLine(m.Value);            }            Console.WriteLine(a);
[解决办法]
不是很了解C#,学习中,不过我觉得这个应该很容易实现
[解决办法]
C# code
string str = "adfdfkeaaaldcleidlfadtuduu ";Regex reg = new Regex(@"a", RegexOptions.Singleline);Console.WriteLine(reg.Matches(str).Count);
[解决办法]
using System;
using System.Collections;
class SetArray
{
static public Hashtable TestStr(string str)
{
char[] a = str.ToCharArray();
Hashtable table = new Hashtable();
for (int i = 0; i < a.Length; i++)
{
if (str.Substring(0, i).IndexOf(a[i]) == -1)
{
int count = 0;
foreach (char x in a)
{
if (x == a[i])
{
count++;
}
}


table.Add(a[i], count);
}
}
return table;
}
static void Main()
{
string a = Console.ReadLine();
Hashtable test = TestStr(a);
foreach (object o in test.Keys)
{
char b = (char)o;
Console.WriteLine("{0}:{1}", b, test[b].ToString());
}
}
}

有点笨。。。
[解决办法]

C# code
using System;using System.Collections;class SetArray{    static public Hashtable TestStr(string str)    {        char[] a = str.ToCharArray();        Hashtable table = new Hashtable();        for (int i = 0; i < a.Length; i++)        {            if (str.Substring(0, i).IndexOf(a[i]) == -1)            {                int count = 0;                foreach (char x in a)                {                    if (x == a[i])                    {                        count++;                    }                }                table.Add(a[i], count);            }        }        return table;    }    static void Main()    {        string a = Console.ReadLine();        Hashtable test = TestStr(a);        foreach (object o in test.Keys)        {            char b = (char)o;            Console.WriteLine("{0}:{1}", b, test[b].ToString());        }    }} 

读书人网 >C#

热点推荐