【问题标题】:Counting consecutive letter pairs in string计算字符串中的连续字母对
【发布时间】:2016-03-18 21:54:53
【问题描述】:

我正在创建一个图表(字符串中 2 个字母(如 aa、ac、bh、is)的频率) 假设文本中没有特殊字符,没有空格。只有字母。

        string text;
        char char1[]= {a,b,c,....z};   //26 alphabets
        int[][] count=null;
        for (i = 0; i < 26; i++)
        {
            for (j = 0; j < 26; j++)
            {
                count[i][j] = text.Count(char[i]char[j]); <---- this is the problem

            }
        }

所以 count[][] 将 hv 文本中所有字母对的出现次数 比如 aa 10,ab 5 n 等等...

for 循环中的语句只是为了说明需要做什么。 我正在考虑使用 foreach 循环,但我们无法在 foreach 中读取两个字符。 我可以使用 switch,但在 switch 中必须写 26x26=676 例大声笑

卡在这里.. 我如何从字符串中读取 2 个字符?并计算它们在整个字符串中的出现次数

【问题讨论】:

    标签: c# string count


    【解决方案1】:

    如果您使用 LINQ,它会成为一个简单的解决方案。字符串上有一个 Split 方法,您可以使用它来拆分数组中的字符串并对其进行迭代。

    using System;
    using System.Linq;
    using System.Collections.Generic;
    
    public class Program
    {
        public static void Main()
        {
            string text = "this is a ss and mm is not to be count an me"; //Input string
    
            // Find only 2 letter strings
            List<string> allTwoLetters = text.Split(new Char[]{' '}).Where(x=>x.Length==2).ToList();
    
    
            //Find all Distinct strings in two letter string list
            List<string> distinctStrings = allTwoLetters.Distinct().ToList();
    
            //dictionary to hold result
            Dictionary<string,int> letterCount = new Dictionary<string,int>();
    
            //Iterate throug each string in distinct string and count how many such strings are there i two letter list of strings
            foreach(string current in distinctStrings)
            {
                letterCount.Add(current,allTwoLetters.Where(p=>p == current).ToList().Count);
            }
    
            //Output values
            foreach(var kvp in letterCount)
            {
                Console.WriteLine(kvp.Key + " - "+ kvp.Value);
            }
        }
    }
    

    【讨论】:

    • 我不知道 Linq :/ n 不想使用我不懂的东西...但非常感谢您的帮助 :)
    • @VishalSingh 这对您在软件开发方面的未来来说并不是一个好兆头。
    • @VishalSingh,不要害怕尝试你不理解的东西,这就是你理解它的方式!
    【解决方案2】:

    使用Regex 尝试类似的操作。

            string text = "aabbcc";
            char[] char1 =  { 'a', 'b', 'c' , 'z' };   //26 alphabets
            int[,] count = new int[char1.Length, char1.Length];
    
            for (int i = 0; i < char1.Length; i++)
            {
                for (int j = 0; j < char1.Length; j++)
                {
                    count[i,j] = Regex.Matches(text, string.Concat(char1[i],char1[j]), RegexOptions.IgnoreCase).Count;
                }
            }
    

    注意,我将 RegexOption 设置为忽略大小写,但如果您想区分大小写,可以将其删除。

    【讨论】:

      【解决方案3】:

      类似于 LINQ 答案,但没有 LINQ。这使用了一个字典,其中两个字母对作为键,一个 int 作为计数。

      string text ="asasdfasdf";
              char[] alphabet = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
              Dictionary<string,int> letterCombos = new Dictionary<string,int>();
              for (int i = 0; i < 26; i++)
              {
                  for (int j = 0; j < 26; j++)
                  {
                      letterCombos.Add(alphabet[i] + alphabet[j].ToString(), 0);
                  }
              }
              char[] textCharArray=text.ToCharArray();
              for (int i =0;i<text.Length-1;i++)
              {
                  string partial = textCharArray[i] + textCharArray[i + 1].ToString();
                  letterCombos[partial]++;
              }
              foreach (KeyValuePair<string, int> kp in letterCombos)
              {
                  Console.WriteLine(kp.Key +": "+kp.Value);
              }
      

      【讨论】:

        猜你喜欢
        • 2018-11-20
        • 1970-01-01
        • 2019-04-30
        • 1970-01-01
        • 2015-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多