本帖最后由 再起来 于 2013-12-17 10:25 编辑
- class Program
- {
- static void Main(string[] args)
- {
- string str = "Welcome to ChinaWorld";
- string[] strs = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);//去掉空格
- string str1 = string.Join("", strs);//将去掉空格的字符串数组拼接成一个字符串
- #region MyRegion
- // HashSet<char> set=new HashSet<char>();
- // foreach (char ch in str1)
- // {
- // set.Add(ch);
- // }
- //char[] chs= set.ToArray();
- //string str2="";
- //for (int i = 0; i < chs.Length;i++ )
- //{
- // str2 += chs[i];
- //}
- #endregion
- int[] count=new int[str1.Length];//定义一个数组,用来存储字符串每个字符出现的次数
- for (int i = 0; i < str1.Length;i++ )//依次找出字符串中每个字符出现的次数
- {
- int time=0;//字符出现的次数
- int position=-1;
- do
- {
- position=str1.IndexOf(str1[i],position+1);
- if (position!=-1)
- {
- time++;
- }
- } while (position!=-1);
- count[i] = time;
- }
- for (int i = 0; i < str1.Length;i++ )
- {
- Console.Write("\"{0}{1}\"\t", str1[i], count[i]);
- }
- Console.ReadKey();
- }
- }
复制代码 |
|