问题:键盘输入如“ABefgt”的字母串,使用C#编程实现计算每个字母出现次数的程序。当键盘输入字符串后,如何遍历和比较字符串中每个字符是否相等呀?(本人认真学习当中)
希望大家用代码支持我下,谢了  

解决方案 »

  1.   


    using System;
    using System.Collections;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;
    namespace ConsoleApplication18
    {
        class Program
        {
            static void Main(string[] args)
            { 
               
              
                Hashtable ht = new Hashtable();
               
               
               
                int count = 0;
                SortedList list = new SortedList();
                string[] str = { "abcd", "我们dcag", "有本事来啊啊hello", "呵呵的hi", "你name", "屁compute", "有本事来啊啊hello", "有本事来啊啊hello", "有本事来啊啊hello", "有本事来啊啊hello", "有本事来啊啊hello", "有本事来啊啊hello", "有本事来啊啊hello", "有本事来啊啊hello", "有本事来啊啊hello", "有本事来啊啊hello", "有本事来啊hello" };           Console.WriteLine( DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff").ToString());           string time1 = DateTime.Now.ToString();
                
                for (int i = 0; i < str.Length; i++)
                {
                    for (int j = 0; j < str[i].Length; j++)
                    {
                        for (int k =i; k < str.Length; k++)
                            for (int l = 0; l < str[k].Length; l++)
                            {
                                if (!ht.Contains(str[i][j].ToString()))
                                {
                                    if (str[i][j] == str[k][l])
                                    {
                                        count++;
                                        str[k].Remove(l,1);
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }                                      if (!ht.Contains(str[i][j].ToString()))
                        {
                                                   ht.Add(str[i][j].ToString(), count.ToString());
                            list.Add(str[i][j].ToString(),count); 
                           
                         
                        } 
                   // str[i].Remove(j,1);                    count = 0;
                       
                    }
                 
                }          
                for (int i = 0; i < list.Count; i++)
                {
                    Console.WriteLine(i.ToString()+":"+list.GetKey(i).ToString()+"-->"+list.GetByIndex(i).ToString());
                }
             /*   IDictionaryEnumerator et = ht.GetEnumerator();            while (et.MoveNext())
                {
                    Console.WriteLine(et.Key.ToString() + "->" + et.Value.ToString());
                   
                }*/
                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
                string time2 = DateTime.Now.ToString();
                Console.WriteLine((DateTime.Parse(time1) - DateTime.Parse(time2)).ToString());
                       }
        }
    }
    这个我以前写的
      

  2.   

    把字符串存在Session里啊
    然后比较,就跟比较验证码一样的
      

  3.   

    当键盘输入字符串后,如何遍历和比较字符串中每个字符是否相等呀? 
    是什么意思呀??
    问题:键盘输入如“ABefgt”的字母串,使用C#编程实现计算每个字母出现次数的程序。 
    答:
    string s=“ABefgt”;
    int acont,bcount,ccount,dcount,...,zcount=0;
    int len=s.length;s=s.replace("a",string.empty);
    acount=len-s.length;
    len=s.length;s=s.replace("b",string.empty);
    bcount=len-s.length;
    len=s.length;s=s.replace("c",string.empty);
    ccount=len-s.length;
    len=s.length;
    .....
    .....
    .....s=s.replace("z",string.empty);
    zcount=len-s.length;
    len=s.length;
      

  4.   


            private void NewMethod()
            {
                List<string> list = new List<string>();//List            string s = string.Empty;
                if (textBox1.Text.Trim() != "")
                {
                    char[] c = textBox1.Text.Trim().ToCharArray();                for (int i = 0; i < c.Length; i++)
                    {
                        int count = 0;
                        for (int j = 0; j < c.Length; j++)//相同的累加
                        {
                            if (c[i] == c[j])
                                count++;
                        }                    s = c[i] + "=" + count;
                        if (list.IndexOf(s) == -1)//如果存在就不用加
                        {
                            list.Add(s);
                            this.textBox2.Text += s + "\r\n";
                        }
                    }
                }
            }
      

  5.   

    fsfdsfdsssssssssddddddddddssss
    结果
    f=3
    s=15
    d=12
    不建议使用本方法
      

  6.   

    谢10#
    List<string> list = new List<string>();//List
    List<string>我没学习过,为什么不建议此方法呢?
      

  7.   

    不是说不要List,是最好不要用这种算法啊。
      

  8.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace Countstring
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<char> list = new List<char>();
                Console.WriteLine("请输入您要查询的字符串:\n");
                string s = Console.ReadLine();
               
                foreach (char ss in s)
                {
                    list.Add(ss);
                }            Console.WriteLine("该字符串中出现的字符的次数分别如下:\n");
                int count=0;
                for (int i = 0; i < list.Count;i++ )
                {
                    //比较该字符在前面是否出现过?是,就跳过,因为前面统计了;否,开始统计
                    int frontifsame = 0;
                    for (int j = i - 1; j >= 0; j--)
                    {
                        if (list[j] == list[i])
                        {
                            frontifsame++;
                            break;//比较该字符在前面是否出现过?是,就跳过,因为前面统计了;
                        }
                    }                if (frontifsame == 0)//比较该字符在前面是否出现过?否,开始统计
                    {
                        count = 1;
                        for (int j = i + 1; j < list.Count; j++)
                        {
                            if (list[j] == list[i])
                            {
                                count++;
                            }
                        }                    Console.WriteLine("{0}  在您输入的字符串中出现的次数为:  {1}.\n", list[i], count);
                    }
                }
                      }
        }
    }
      

  9.   

    不用list,用list是多余的。using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace Countstring
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("请输入您要查询的字符串:\n");
                string s = Console.ReadLine();
          
                Console.WriteLine("该字符串中出现的字符的次数分别如下:\n");
                int count=0;
                for (int i = 0; i < s.Length;i++ )
                {
                    //比较该字符在前面是否出现过?是,就跳过,因为前面统计了;否,开始统计
                    int frontifsame = 0;
                    for (int j = i - 1; j >= 0; j--)
                    {
                        if (s[j] == s[i])
                        {
                            frontifsame++;
                            break;//比较该字符在前面是否出现过?是,就跳过,因为前面统计了;
                        }
                    }                if (frontifsame == 0)//比较该字符在前面是否出现过?否,开始统计
                    {
                        count = 1;
                        for (int j = i + 1; j < s.Length; j++)
                        {
                            if (s[j] == s[i])
                            {
                                count++;
                            }
                        }                    Console.WriteLine("{0}  在您输入的字符串中出现的次数为:  {1}.\n", s[i], count);
                    }
                }
                      }
        }
    }
      

  10.   

    如果只统计英文字母,并且区分大小写,
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace Countstring
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("请输入您要查询的字符串:\n");
                string s = Console.ReadLine();
          
                Console.WriteLine("该字符串中出现的字符的次数分别如下:\n");
                int count=0;
                for (int i = 0; i < s.Length;i++ )
                {
                    if (s[i] < 65 || s[i] > 122||(s[i]>=90&&s[i]<97))//判断要统计的字符是否是英文字母,如果不是,就跳过不统计
                    {
                        break;
                    }
                    //比较该字符在前面是否出现过?是,就跳过,因为前面统计了;否,开始统计
                    int frontifsame = 0;
                    for (int j = i - 1; j >= 0; j--)
                    {
                        if (s[j] == s[i])
                        {
                            frontifsame++;
                            break;//比较该字符在前面是否出现过?是,就跳过,因为前面统计了;
                        }
                    }                if (frontifsame == 0)//比较该字符在前面是否出现过?否,开始统计
                    {
                        count = 1;
                        for (int j = i + 1; j < s.Length; j++)
                        {
                            if (s[j] == s[i])
                            {
                                count++;
                            }
                        }                    Console.WriteLine("{0}  在您输入的字符串中出现的次数为:  {1}.\n", s[i], count);
                    }
                }
                      }
        }
    }
      

  11.   

    using System;
    using System.Linq;namespace ConsoleApplication4
    {
        class Program
        {
            static void Main(string[] args)
            {
                string str = "asdoisjdfoiajsdfoijasdjssdfsdf";
                var result = from item in str group item by item into groups select groups;            foreach (var charResult in result)
                    Console.WriteLine("{0}, {1}", charResult.Key, charResult.Count());
            }
        }
    }
      

  12.   

    键盘输入如“ABefgt”的字母串,使用C#编程实现计算每个字母出现次数的程序。
    都不很好 用sortliststatic void Main(string[] args)
            {
                string data = "asd646uio4asda .sdjhglajsdoajam] 4uio64aaaksd;ka;s";
                SortedList<char, int> charCount = new SortedList<char, int>();
                foreach (char i in data) 
                {
                    if (!charCount.ContainsKey(i))
                    {
                        charCount.Add(i, 1);
                    }
                    else 
                    {
                        charCount[i]=charCount[i]+1;
                    }
                }            foreach (KeyValuePair<char, int> i in charCount)
                {
                    Console.WriteLine(" "+i.Key+": "+i.Value);
                }
            }保证可以用 还是排序好了的
      

  13.   

    其实SortedList效率很低的,主要慢在添加charCount.Add(i, 1);这类问题linq效率是最高的,比自己开int[26]或Dictinary还高!已经有人测过了,
    我也试过,虽然以前一直不太相信。可以用个10K左右的字符串试试!想排序也很容易var result = from item in str group item by item into groups orderby groups.Count() descending select groups;按照字符重复次数排序从高到低var result = from item in str group item by item into groups orderby groups.Key select groups;按照字符排序
      

  14.   

    【原创家庭记账簿1.1,简单实用,欢迎工薪阶层、家庭主妇下载使用
    http://download.csdn.net/source/1842593】
      

  15.   

    2者在处理大量数据时都不很理想
    linq对于初学者还是有点难理解的 相对sortlist要难点 sortlist跟hash'表差不多 容易理解
      

  16.   

    SortedList效率同hash差的比较远,你说的应该是SortedDictionary吧!
      

  17.   

    "AbedA".Length-"Abed".Replace("A","")就是A出现的次数
      

  18.   

    public static void main(String[] args) { Scanner input=new Scanner(System.in);
    System.out.print("请输入你字符串");
    String text=input.nextLine();

    Map map=new HashMap(); for(int i=0;i<text.length();i++){ String key=text.charAt(i)+""; if(map.containsKey(key)){
    int val=Integer.valueOf(map.get(key)+"")+1;
    map.put(key,val);
    }else{
    map.put(key, 1);
    }
    } Iterator key=map.entrySet().iterator(); while(key.hasNext()){
    Map.Entry entry=(Map.Entry)key.next();
    Object keys=entry.getKey();
    Object value=entry.getValue();
    System.out.println(keys+"的个数为"+value);
    }
    }
      

  19.   

    粗略做了个,lz看看
     class Program
        {
            public static int numWord(string word, string[] wordList)//查询字母在字符数组中出现的次数查询
            {
                int cout = 0;
                for (int i = 0; i < wordList.Length; i++)
                {
                    if (word.Equals(wordList[i], StringComparison.Ordinal))
                    cout++;
                }
                return cout;
            }
            static void Main(string[] args)
            {
                int cout ;   //定义字母出现的查询
                string word; //要查询的字母
                string strWordlist;//要输入的字符串
                string[] wordList = new string[100];
                Console.WriteLine("请输入字符串数组");
                strWordlist = Console.ReadLine();
                for (int i = 0; i < strWordlist.Length; i++) //讲输入的字符串放入字符数组中
                {
                    wordList[i] = strWordlist.Substring(i, 1);
                }
                Console.WriteLine("请输入你要查询的字母");
                word = Console.ReadLine();
                cout=numWord(word,wordList);
                Console.WriteLine(cout);
                Console.ReadLine();
            }
      

  20.   

    这个保证不难理解
    class Program
        {
            static void Main(string[] args)
            {
                string a = "asdfeDSds";
                for (int i = 'A'; i <= 'Z'; i++)
                {
                    Console.WriteLine(((char)i).ToString()+":"+(a.Length-a.Replace(((char)i).ToString(),"").Length));
                }
                for (int i = 'a'; i <= 'z'; i++)
                {
                    Console.WriteLine(((char)i).ToString() + ":" + (a.Length - a.Replace(((char)i).ToString(), "").Length));
                }
            }
        }
      

  21.   

    另外正则似乎效率似乎比replace稍微低一些
      

  22.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace ConsoleApplication17
    {
        class Program
        {
           static int a = 0;
            static void Main(string[] args)
            {
                Console.WriteLine(sumA());
            }
            static int sumA()
            {
               
                char[] text = Console.ReadLine().ToString().ToCharArray();
                for (int i = 0; i < text.Length; i++)
                {
                    if (text[i] == 'a')
                        a++;            }
                return a;
            }
        }
    }
      

  23.   

     var query =
                    from ch in strTest
                    group ch by ch into gr                                              select gr;            foreach (var groupText in query)
                    Console.Write( (char)groupText.Key);            Console.WriteLine();
                foreach (var groupText in query)
                {
                    Console.Write( groupText.Count());
                }
                Console.ReadLine();