2016-03-18 11 views
0

私は文字列中に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 

      } 
     } 

ので、forループ内のステートメントはただで

... [] []ようにnはAB 5、AA 10のようなテキスト にアルファベットのすべてのペアの出現回数をカウントし、HVう何が必要なのかを知ることができます。 私はforeachループを使用することを考えていましたが、foreachでは2文字は読み込めません。 私はスイッチを使用することができますが、スイッチは26x26 = 676ケースを書く必要があります。

ここにこだわってしまった。 どのように文字列から2文字を読みますか?文字列全体での出現回数を数えます。

答えて

0

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を入れていますが、大文字と小文字を区別したい場合は、それを削除することができます。

1

LINQを使用すると簡単に解決できます。 stringに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); 
     } 
    } 
} 
+0

私は、LINQの知らない:/ n iは理解しないものを使用したくない... しかし、ウルの助けに感謝をたくさん:)ソフトウェア開発にあなたの将来のためによく前兆はありません –

+1

@VishalSinghを。 –

+1

@ VishalSingh、あなたが理解していないことを恐れないでください、それはあなたがそれを理解する方法です! – allie

0

LINQの回答に似ていますが、LINQはありません。これは、2つの文字ペアをキーとする辞書と、カウントとしての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); 
     } 
関連する問題