求算一字符串中独立字符的个数。
如string str="jsffff";
其中
j,1
s,1
f,4
不才,在下抛砖引玉了............
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace 字符统计
{
class Program
{
static void Main(string[] args)
{
string myString = "uhifhiohskljhdsafipo喔喔 哈哈 你好啊";
Dictionary <char, int> myDictionary = MyMethod.GetInfoFormString(myString);
foreach(KeyValuePair <char,int> kvp in myDictionary)
{
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
}
} }
public static class MyMethod
{
public static Queue <char> GetOnlyCharFromString(string str)
{
Queue <char> myQueue = new Queue <char>();
for (int i = 0; i < str.Length; i++)
{
if (myQueue.Contains(str[i]) == false)
myQueue.Enqueue(str[i]);
}
return myQueue;
}
public static Dictionary <char,int> GetInfoFormString(string str)
{
Queue <char> myQueue = GetOnlyCharFromString(str );
Dictionary <char,int> myDictionary=new Dictionary <char,int>();
int oldQueueCount = myQueue.Count;//记录下它原有的量;
for(int i=0;i <oldQueueCount ;i++)
{
char myChar=myQueue.Dequeue();
int num=0;
for(int j=0;j <str.Length;j++)
{
if(str[j]==myChar)
num++;
}
myDictionary.Add(myChar,num);
}
return myDictionary;
} } }
如string str="jsffff";
其中
j,1
s,1
f,4
不才,在下抛砖引玉了............
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace 字符统计
{
class Program
{
static void Main(string[] args)
{
string myString = "uhifhiohskljhdsafipo喔喔 哈哈 你好啊";
Dictionary <char, int> myDictionary = MyMethod.GetInfoFormString(myString);
foreach(KeyValuePair <char,int> kvp in myDictionary)
{
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
}
} }
public static class MyMethod
{
public static Queue <char> GetOnlyCharFromString(string str)
{
Queue <char> myQueue = new Queue <char>();
for (int i = 0; i < str.Length; i++)
{
if (myQueue.Contains(str[i]) == false)
myQueue.Enqueue(str[i]);
}
return myQueue;
}
public static Dictionary <char,int> GetInfoFormString(string str)
{
Queue <char> myQueue = GetOnlyCharFromString(str );
Dictionary <char,int> myDictionary=new Dictionary <char,int>();
int oldQueueCount = myQueue.Count;//记录下它原有的量;
for(int i=0;i <oldQueueCount ;i++)
{
char myChar=myQueue.Dequeue();
int num=0;
for(int j=0;j <str.Length;j++)
{
if(str[j]==myChar)
num++;
}
myDictionary.Add(myChar,num);
}
return myDictionary;
} } }
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货