2011-12-16 26 views
0

在輸入處我有一些字符串:「今天下雪知道」,在這裏我有3個單詞,所以我必須解析它們是這樣的:每個角色我必須與所有其他角色比較,並且總和這些詞有多少個相同的字符,比如「o」字母的例子是2(來自「今天」和「下雪」)或者「w」字母將是2(來自「知道」和「下雪」)。之後,我必須用字母數字(轉換爲字符格式)替換這些字符。結果應該是「13111 133211 1332」。解析字符串並替換字母Java

我做了什麼?

首先我一卷錄音帶,一些單詞和

public void inputStringsForThreads() { 

     boolean flag; 

      do { 

    // will invite to input 
       stringToParse = Input.value(); 

       try { 

       flag = true; 

    // in case that found nothing , space , number and other special character , throws an exception 
       if (stringToParse.equals("") | stringToParse.startsWith(" ") | stringToParse.matches(".*[0-9].*") | stringToParse.matches(".*[~`[email protected]#$%^&*()-+={};:',.<>?/'_].*")) 

        throw new MyStringException(stringToParse); 

       else analizeString(stringToParse);  
      } 

      catch (MyStringException exception) { 

       stringToParse = null; 
       flag = false; 
       exception.AnalizeException(); 
      } 
      } 
      while (!flag); 
} 

我消除單詞之間的空格,並從這些話讓只是一個

static void analizeString (String someString) { 

// + sign treat many spaces as one 
     String delimitator = " +"; 

// words is a String Array 
     words = someString.split(delimitator); 

// temp is a string , will contain a single word 
     temp = someString.replaceAll("[^a-z^A-Z]",""); 


     System.out.println("=============== Words are : ==============="); 
     for (int i=0;i<words.length;i++) 
      System.out.println((i+1)+")"+words[i]); 
    } 

所以我儘量對每一個字部分的每一個字比較(被分成字母)與所有字母的所有字母,但我不知道如何計算同一個字母的數字,並在每個字母的正確數字替換字母后?有任何想法嗎 ?

// this will containt characters for every word in part 
     char[] motot = words[id].toCharArray(); 

// this will containt all characters from all words  
     char[] notot = temp.toCharArray(); 


    for (int i =0;i<words[i].length();i++) 

       for (int j=0;j<temp.length ;j++) 

       { 
        if (i == j) { 

         System.out.println("Same word"); 

        } 

        else if (motot[i] == notot[j]) { 

         System.out.println("Found equal :"+lol[i]+" "+lol1[j]); 

        }} 
+0

嘗試'HashMap <字符,整數>'。 – Kevin

+2

這功課嗎? – Perception

+0

大寫/小寫字母怎麼樣(它們是否被認爲是用於計數的相同字符)? –

回答

3

計數你可能想使用Map<Character, Integer> counterjava.util.HashMap。如果使用計數器的特定鍵(字符)獲取值(整數)爲'非空',那麼您的值爲++(利用自動裝箱)。否則在計數器中放入一個新條目(char,1)。

用數字替換字母應該是相當容易的。

+0

+1,但更簡單的方法可能只是使用大小爲26的數組並在index = character處增加值'a'...在迭代結束時,您將擁有計數。 – aishwarya

0

這是更好地使用模式匹配這樣的:

最初..

private Matcher matcher; 
Pattern regexPattern = Pattern.compile(pattern); 
matcher = regexPattern.matcher(""); 

的多模式匹配。

private final String[] patterns = new String [] {/* instantiate patterns here..*/} 
private Matcher matchers[]; 
for (int i = 0; i < patterns.length; i++) { 
Pattern regexPattern = Pattern.compile(pattern[i]); 
matchers[i] = regexPattern.matcher(""); 

} 

,然後匹配模式..你多匹配檢查做..

if(matcher.reset(charBuffer).find()) {//matching pattern.} 

for (int i = 0; i < matchers.length; i++) if(matchers[i].reset(charBuffer).find()) {//matching pattern.} 

不要使用字符串匹配,效率不高。

始終使用CharBuffer而不是String。

0

下面是一些C#代碼(這是相當類似於Java):

void replace(string s){ 

    Dictionary<char, int> counts = new Dictionary<char, int>(); 

    foreach(char c in s){ 
     // skip spaces 
     if(c == ' ') continue; 

     // update count for char c 
     if(!counts.ContainsKey(c)) counts.Add(c, 1); 
     else counts[c]++; 
    } 

    // replace characters in s 
    for(int i = 0; i < s.Length; i++) 
     if(s[i] != ' ') 
      s[i] = counts[s[i]]; 
} 

注重在第二循環中不變的字符串。可能要使用某種StringBuilder

0

以下是僅適用於小寫字符串的解決方案。可怕的可怕的代碼,但我試圖看看我可以寫一個解決方案有多少行。

public static String letterCount(String in) { 
    StringBuilder out = new StringBuilder(in.length() * 2); 
    int[] count = new int[26]; 
    for (int t = 1; t >= 0; t--) 
    for (int i = 0; i < in.length(); i++) { 
     if (in.charAt(i) != ' ') count[in.charAt(i) - 'a'] += t; 
     out.append((in.charAt(i) != ' ') ? "" + count[in.charAt(i) - 'a'] : " "); 
    } 
    return out.substring(in.length()); 
}