2012-06-11 70 views
1

我已經想出了這個代碼,它適用於老師想要的東西。計算空格,計算單詞,進行子字符串搜索並單獨計算字母,並顯示使用哪些字母。在字符串中使用數組

但我需要將其轉換爲數組方法而不是600行的while循環。我真的不知道如何做到這一點。任何人都可以給我輸入嗎?

using System; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      int a = 0; 
      int b = 0; 
      int c = 0; 
      int d = 0; 
      int e = 0; 
      int f = 0; 
      int g = 0; 
      int h = 0; 
      int i = 0; 
      int j = 0; 
      int k = 0; 
      int l = 0; 
      int m = 0; 
      int n = 0; 
      int o = 0; 
      int p = 0; 
      int q = 0; 
      int r = 0; 
      int s = 0; 
      int t = 0; 
      int u = 0; 
      int v = 0; 
      int w = 0; 
      int x = 0; 
      int y = 0; 
      int z = 0; 

      int A = 0; 
      int B = 0; 
      int C = 0; 
      int D = 0; 
      int E = 0; 
      int F = 0; 
      int G = 0; 
      int H = 0; 
      int I = 0; 
      int J = 0; 
      int K = 0; 
      int L = 0; 
      int M = 0; 
      int N = 0; 
      int O = 0; 
      int P = 0; 
      int Q = 0; 
      int R = 0; 
      int S = 0; 
      int T = 0; 
      int U = 0; 
      int V = 0; 
      int W = 0; 
      int X = 0; 
      int Y = 0; 
      int Z = 0; 
      int readChar = 0; 
      int word = 0; 
      int lower = 0; 
      int upper = 0; 
      string inputString =""; 
      char ch = ' '; 
      string findString = ""; 
      int space = 0; 
      int startingPoint = 0; 
      int findStringCount = 0; 


      Console.Write("Please enter a string: "); 


      do{ 
       readChar = Console.Read(); 

       ch = Convert.ToChar(readChar); 
       if (ch.Equals(' ')) 
       { 
        space++; 
       } 
       else if (Char.IsLower(ch)) 
       { 
        lower++; 
        if (ch.Equals('a')) 
        { 
         a++; 
        } 
        else if (ch.Equals('b')) 
        { 
         b++; 
        } 
        else if (ch.Equals('c')) 
        { 
         c++; 
        } 
        else if (ch.Equals('d')) 
        { 
         d++; 
        } 
        else if (ch.Equals('e')) 
        { 
         e++; 
        } 
        else if (ch.Equals('f')) 
        { 
         f++; 
        } 
        else if (ch.Equals('g')) 
        { 
         g++; 
        } 
        else if (ch.Equals('h')) 
        { 
         h++; 
        } 
        else if (ch.Equals('i')) 
        { 
         i++; 
        } 
        else if (ch.Equals('j')) 
        { 
         j++; 
        } 
        else if (ch.Equals('k')) 
        { 
         k++; 
        } 
        else if (ch.Equals('l')) 
        { 
         l++; 
        } 
        else if (ch.Equals('m')) 
        { 
         m++; 
        } 
        else if (ch.Equals('n')) 
        { 
         n++; 
        } 
        else if (ch.Equals('o')) 
        { 
         o++; 
        } 
        else if (ch.Equals('p')) 
        { 
         p++; 
        } 
        else if (ch.Equals('q')) 
        { 
         q++; 
        } 
        else if (ch.Equals('r')) 
        { 
         r++; 
        } 
        else if (ch.Equals('s')) 
        { 
         s++; 
        } 
        else if (ch.Equals('t')) 
        { 
         t++; 
        } 
        else if (ch.Equals('u')) 
        { 
         u++; 
        } 
        else if (ch.Equals('v')) 
        { 
         v++; 
        } 
        else if (ch.Equals('w')) 
        { 
         w++; 
        } 
        else if (ch.Equals('x')) 
        { 
         x++; 
        } 
        else if (ch.Equals('y')) 
        { 
         y++; 
        } 
        else if (ch.Equals('z')) 
        { 
         z++; 
        } 
       } 
       else if (Char.IsUpper(ch)) 

       { 
        upper++; 
        if (ch.Equals('A')) 
        { 
         A++; 
        } 
        else if (ch.Equals('B')) 
        { 
         B++; 
        } 
        else if (ch.Equals('C')) 
        { 
         C++; 
        } 
        else if (ch.Equals('D')) 
        { 
         D++; 
        } 
        else if (ch.Equals('E')) 
        { 
         E++; 
        } 
        else if (ch.Equals('F')) 
        { 
         F++; 
        } 
        else if (ch.Equals('G')) 
        { 
         G++; 
        } 
        else if (ch.Equals('H')) 
        { 
         H++; 
        } 
        else if (ch.Equals('I')) 
        { 
         I++; 
        } 
        else if (ch.Equals('J')) 
        { 
         J++; 
        } 
        else if (ch.Equals('K')) 
        { 
         K++; 
        } 
        else if (ch.Equals('L')) 
        { 
         L++; 
        } 
        else if (ch.Equals('M')) 
        { 
         M++; 
        } 
        else if (ch.Equals('N')) 
        { 
         N++; 
        } 
        else if (ch.Equals('O')) 
        { 
         O++; 
        } 
        else if (ch.Equals('P')) 
        { 
         P++; 
        } 
        else if (ch.Equals('Q')) 
        { 
         Q++; 
        } 
        else if (ch.Equals('R')) 
        { 
         R++; 
        } 
        else if (ch.Equals('S')) 
        { 
         S++; 
        } 
        else if (ch.Equals('T')) 
        { 
         T++; 
        } 
        else if (ch.Equals('U')) 
        { 
         U++; 
        } 
        else if (ch.Equals('V')) 
        { 
         V++; 
        } 
        else if (ch.Equals('W')) 
        { 
         W++; 
        } 
        else if (ch.Equals('X')) 
        { 
         X++; 
        } 
        else if (ch.Equals('Y')) 
        { 
         Y++; 
        } 
        else if (ch.Equals('Z')) 
        { 
         Z++; 
        } 
       } 

       if (((ch.Equals(' ') && (!inputString.EndsWith(" ")))||(ch.Equals('\r') && (!inputString.EndsWith(" "))))&&(inputString!="")) 
       { 
        word++; 
       } 

       inputString = inputString + ch; 

      } while (ch != '\r'); 

      Console.ReadLine(); 

      Console.WriteLine("Report on {0}",inputString); 

      Console.WriteLine("# of spaces {0}",space); 
      Console.WriteLine("# of lower {0}", lower); 
      Console.WriteLine("# of upper {0}", upper); 
      Console.WriteLine("# of word {0}", word); 
      Console.WriteLine("UPPERCASE"); 
      if (A >= 1) 
      { 
       Console.WriteLine("A = {0}",A); 
      } 
      if (B >= 1) 
      { 
       Console.WriteLine("B = {0}",B); 
      } 
      if (C >= 1) 
      { 
       Console.WriteLine("C = {0}", C); 
      } 
      if (D >= 1) 
      { 
       Console.WriteLine("D = {0}", D); 
      } 
      if (E >= 1) 
      { 
       Console.WriteLine("E = {0}", E); 
      } 
      if (F >= 1) 
      { 
       Console.WriteLine("F = {0}", F); 
      } if (G >= 1) 
      { 
       Console.WriteLine("G = {0}", G); 
      } 
      if (H >= 1) 
      { 
       Console.WriteLine("H = {0}", H); 
      } 
      if (I >= 1) 
      { 
       Console.WriteLine("I = {0}", I); 
      } 
      if (J >= 1) 
      { 
       Console.WriteLine("J = {0}", J); 
      } 
      if (K >= 1) 
      { 
       Console.WriteLine("K = {0}", K); 
      } 
      if (L >= 1) 
      { 
       Console.WriteLine("L = {0}", L); 
      } 
      if (M >= 1) 
      { 
       Console.WriteLine("M = {0}", M); 
      } 
      if (N >= 1) 
      { 
       Console.WriteLine("N = {0}",N); 
      } 
      if (O >= 1) 
      { 
       Console.WriteLine("O = {0}",O); 
      } 
      if (P >= 1) 
      { 
       Console.WriteLine("P = {0}",P); 
      } 
      if (Q >= 1) 
      { 
       Console.WriteLine("Q = {0}",Q); 
      } 
      if (R >= 1) 
      { 
       Console.WriteLine("R = {0}",R); 
      } 
      if (S >= 1) 
      { 
       Console.WriteLine("S = {0}",S); 
      } 
      if (T >= 1) 
      { 
       Console.WriteLine("T = {0}",T); 
      } 
      if (U >= 1) 
      { 
       Console.WriteLine("U = {0}",U); 
      } 
      if (V >= 1) 
      { 
       Console.WriteLine("V = {0}",V); 
      } 
      if (W >= 1) 
      { 
       Console.WriteLine("W = {0}",W); 
      } 
      if (X >= 1) 
      { 
       Console.WriteLine("X = {0}",X); 
      } 
      if (Y >= 1) 
      { 
       Console.WriteLine("Y = {0}",Y); 
      } 
      if (Z >= 1) 
      { 
       Console.WriteLine("Z = {0}",Z); 
      } 

      Console.WriteLine("LOWERCASE"); 
      if (a >= 1) 
      { 
       Console.WriteLine("a = {0}", a); 
      } 
      if (b >= 1) 
      { 
       Console.WriteLine("b = {0}", b); 
      } 
      if (c >= 1) 
      { 
       Console.WriteLine("c = {0}", c); 
      } 
      if (d >= 1) 
      { 
       Console.WriteLine("d = {0}", d); 
      } 
      if (e >= 1) 
      { 
       Console.WriteLine("e = {0}", e); 
      } 
      if (f >= 1) 
      { 
       Console.WriteLine("f = {0}", f); 
      } if (g >= 1) 
      { 
       Console.WriteLine("g = {0}", g); 
      } 
      if (h >= 1) 
      { 
       Console.WriteLine("h = {0}", h); 
      } 
      if (i >= 1) 
      { 
       Console.WriteLine("i = {0}", i); 
      } 
      if (j >= 1) 
      { 
       Console.WriteLine("j = {0}", j); 
      } 
      if (k >= 1) 
      { 
       Console.WriteLine("k = {0}", k); 
      } 
      if (l >= 1) 
      { 
       Console.WriteLine("l = {0}", l); 
      } 
      if (m >= 1) 
      { 
       Console.WriteLine("m = {0}", m); 
      } 
      if (n >= 1) 
      { 
       Console.WriteLine("n = {0}", n); 
      } 
      if (o >= 1) 
      { 
       Console.WriteLine("o = {0}", o); 
      } 
      if (p >= 1) 
      { 
       Console.WriteLine("p = {0}", p); 
      } 
      if (q >= 1) 
      { 
       Console.WriteLine("q = {0}", q); 
      } 
      if (r >= 1) 
      { 
       Console.WriteLine("r = {0}", r); 
      } 
      if (s >= 1) 
      { 
       Console.WriteLine("s = {0}", s); 
      } 
      if (t >= 1) 
      { 
       Console.WriteLine("t = {0}", t); 
      } 
      if (u >= 1) 
      { 
       Console.WriteLine("u = {0}", u); 
      } 
      if (v >= 1) 
      { 
       Console.WriteLine("v = {0}", v); 
      } 
      if (w >= 1) 
      { 
       Console.WriteLine("w = {0}", w); 
      } 
      if (x >= 1) 
      { 
       Console.WriteLine("x = {0}", x); 
      } 
      if (y >= 1) 
      { 
       Console.WriteLine("y = {0}", y); 
      } 
      if (z >= 1) 
      { 
       Console.WriteLine("z = {0}", z); 
      } 
      Console.WriteLine(); 

      Console.Write("Please enter a substring "); 
      findString = Console.ReadLine(); 


      if (findString.Length <= inputString.Length) 
      { 
       do 
       { 
        if (inputString.IndexOf(findString, startingPoint) != -1) 
        { 
         findStringCount++; 
         startingPoint = inputString.IndexOf(findString, startingPoint) + findString.Length; 
        } 

       } while (inputString.IndexOf(findString, startingPoint) != -1); 
      } 
      else 
      { 
       Console.WriteLine("Substring is too long!"); 
      } 
      Console.WriteLine("The number of times that {0} is found in the text is {1}", findString, findStringCount); 

      Console.ReadLine(); 

     } 
    } 
} 

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.IO; 


namespace ConsoleApplication1 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 


     int[] counterArray = new int[123]; 
     string myString; 
     int wordCounted = 0; 
     int result = 0; 
     //Prompt user and get value 
     Console.Write("Please enter a string: "); 
     myString = 
     Console.ReadLine(); 
     //Word count 
     for (int i = 1; i < myString.Length; i++) 
     { 

      if (char.IsWhiteSpace(myString[i - 1])) 
      { 

       if (char.IsLetterOrDigit(myString[i]) || 
       char.IsPunctuation(myString[i])) 
       { 

        wordCounted++; 

       } 

      } 

     } 

     if (myString.Length > 2) 
     { 

      wordCounted++; 

     } 

     //White space count 
     foreach (char countSpace in myString) 
     { 

      if (char.IsWhiteSpace(countSpace)) 
      { 

       result++; 

      } 

     } 

     //Display words and space count, 
     Console.WriteLine("\nWORDS:\t\t{0}", wordCounted); 
     Console.WriteLine("SPACES: \t{0}", result); 
     for (int x = 0; x < myString.Length; x++) 
     { 

      int myValue = Convert.ToInt32(myString[x]); 
      counterArray[myValue]++; 

     } 

     //Display uppercase letter count 
     Console.WriteLine("\nUPPERCASE LETTERS: "); 
     //Counting uppercase letter 
     for (int y = 65; y < 91; y++) 
     { 

      if (counterArray[y] > 0) 
      { 

       Console.WriteLine("\t\t{0}: \t{1}", Convert.ToChar(y), counterArray[y]); 
      } 

     } 

     //Display lowercase letter count 

     Console.WriteLine("LOWERCASE LETTERS: "); 
     //Counting lowercase letter 
     for (int z = 97; z < 123; z++) 
     { 

      if (counterArray[z] > 0) 
      { 

       Console.WriteLine("\t\t{0}: \t{1}", Convert.ToChar(z), counterArray[z]); 
      } 

     } 

     int startingPoint = 0; 
     int findStringCount = 0; 

     Console.Write("Please enter a substring "); 
     findString = Console.ReadLine(); 

     Console.WriteLine("Press ENTER to exit..."); 

     Console.ReadLine(); 
     } 

    } 

} 
+0

幫助你是什麼意思時,你說:「它轉換成一個陣列的方法「?你想重構你的代碼來使用一個數組而不是無數個變量嗎? –

+0

你甚至可以沒有整個,如果有一個簡單的字典或索引數組(每個索引代表一個字母)的瘋狂 – YavgenyP

+1

@Ken他發佈的答案應該不會對他被問到的問題有任何影響。這個答案是低估的(正確的),所以現在他想擴大他的知識。我說,對他有好處。 – Didaxis

回答

1

嘗試使用哈希表...他們關鍵是字母,該值是它發生

的次數,如果(ht.Contains({} yourletter ))// 通過增加1

 if (ht.ContainsKey(letter)) 
      ht[letter] = Convert.ToInt32(ht[letter]) + 1; 
     else 
      ht.Add(letter, 1); 
+0

爲什麼不使用Dictionary而不是Hashtable? – Martheen

+0

@Martheen我習慣了Hashtables .....還沒有使用過dictionarys,所以不要先想它。 – ganders

2

那麼恕我直言,最簡單的方法的價值來獲得字符數組是這樣的:

char[] alphaLower = "abcdefghijklmnopqrstuvwxyz".ToCharArray(); 
char[] alphaUpper = //same thing as above with uppercase letters; 

此時您可以在循環內進行計算。這樣的事情:

foreach(char c in alphaLower) 
{ 
    //possibly a nested foreach here to process each character of input against each char in the alphabet? 
    //write your calculation to console 
} 

我不會感到驚訝的是,看到上面只有20行代碼重寫了什麼。發佈您在下一次迭代中提出的內容,我們將繼續指引您朝着正確的方向發展。

+0

我們可以直接btw來foreach一個字符串。 – Martheen

4

理想的解決方案是使用Dictionary<char,int>,但是如果作業特別需要數組,則可以使用字母字符的ASCII表示是串行的這一事實。即,A = 65,B = 66,...,Z = 90和a = 97,b = 98,...,z = 122。例如:

var uppercase = new int[26]; 
var lowercase = new int[26]; 

if(ch >= 'A' && ch <= 'Z') 
    uppercase[ch-'A']++; 

if(ch >= 'a' && ch <= 'z') 
    lowercase[ch-'a']++; 

然後,當你去把它打印出來,你可以施放的indeces鍵入char

for(var i='A'; i<='Z'; i++) { 
    Console.WriteLine((char)i + " = " + uppercase[i-'A']); 
} 

我將離開執行其他由你。

+2

我認爲如果你使用字符文字(它們相當於字符代碼)會更清楚:'if(ch> ='A'&& ch <='Z') uppercase [ch - 'A'] + +;' – MRAB

+0

好點,@MRAB。 –

+0

我相信@MRAB也有條件部分。 :) – Groo

5

這是一種使用字典和Linq的方法。 (編輯添加的大寫LINQ操作填寫AZ恭維AZ):

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

Enumerable.Range(0,26) 
      .Select(indx => (char)+('a' + indx)) 
      .Union(Enumerable.Range(0,26) 
          .Select(indx => (char)+('A' + indx))) 
      .ToList() 
      .ForEach(chr => myLetters.Add(chr, 0)); 

myLetters['a'] = 2; 
myLetters['Z'] = 3; 

myLetters.ToList() 
     .ForEach(ml => Console.WriteLine("{0} : {1}", ml.Key, ml.Value)); 

/* Prints out 
a : 2 
b : 0 
c : 0 
... 
Z : 3 
*/ 

注意上面的是學習的目的,我真的做myLetters分配與枚舉一舉這樣如:

Dictionary<char, int> myLetters = 
     Enumerable.Range(0,26) 
       .Select(indx => (char)+('a' + indx)) 
       .Union(Enumerable.Range(0,26) 
           .Select(indx => (char)+('A' + indx))) 
       .ToDictionary (letter => letter, letter => 0); 
1

ASCII空間對於大寫字母是連續的,而對於小寫字母,大寫字母A從小數點開始65。 見http://www.asciitable.com/對於細節

應該分配爲所有字符的數組中ASCII表的下部(127) VAR countingchars =新INT [128];

然後你循環字符串中的字符並做這樣的循環: countingchars [(byte)currentchar] ++;

每當你遇到(字節)currentchar == 32,你應該增加字數

當你走過整個字符串了, 得到countingchars空間[32] 65獲得帽 - 90 得到小97-122字母

希望這已經足夠了。

我留給了你把它很好地轉換成C#

+0

這可能是老師的意思,並且你已經解釋了足夠的,但不是太多的作業問題。 – martinstoeckli

0

希望這將在學習

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 

namespace HomeWork 
{ 
    class Alphabets 
    { 
     static void Main(string[] args) 
     { 
      string sentence = null; 
      string findString = null; 
      int numericCount; 
      int upperCount; 
      int lowerCount; 
      int specialCount; 
      int countBlankSpace; 
      int countWord; 
      Console.Write("Please enter a string: "); 
      sentence = Console.ReadLine(); 
      numericCount = Regex.Matches(sentence, @"\d").Count; 
      upperCount = Regex.Matches(sentence, @"[A-Z]").Count; 
      lowerCount = Regex.Matches(sentence, @"[a-z]").Count; 
      specialCount = Regex.Matches(sentence, @"[!""£$%^&*())]").Count; 
      countBlankSpace = new Regex(" ").Matches(sentence).Count; 
      countWord = Regex.Matches(sentence, @"[\S]+").Count; 
      Console.WriteLine("Report on {0}",sentence); 
      Console.WriteLine("# of spaces {0}", countBlankSpace); 
      Console.WriteLine("# of lower {0}", lowerCount); 
      Console.WriteLine("# of upper {0}", upperCount); 
      Console.WriteLine("# of word {0}", countWord); 
      Console.WriteLine("# of Special Characters {0}", specialCount); 
      Console.Write("Please enter a substring:"); 
      findString = Console.ReadLine(); 
      Alphabets.findSubString(findString, sentence); 
      Console.WriteLine("\nLowercase Letters \n"); 
      Alphabets.lowerLetters(sentence); 
      Console.WriteLine("\nUppercase Letters \n"); 
      Alphabets.upperLetters(sentence); 
      Console.ReadLine(); 
     } 
     public static void lowerLetters(string sentence) 
     { 
      int[] upper = new int[(int)char.MaxValue]; 
      // 1. 
      // Iterate over each character. 
      foreach (char t in sentence) 
      { 
       // Increment table. 
       upper[(int)t]++; 
      } 

      // 2. 
      // Write all letters found. 
      for (int i = 0; i < (int)char.MaxValue; i++) 
      { 
       if (upper[i] > 0 && 
       char.IsLower((char)i)) 
       { 
        Console.WriteLine("Letter: {0} = {1}", 
         (char)i, 
         upper[i]); 
       } 
      } 
     } 

     public static void upperLetters(string sentence) 
     { 
      int[] upper = new int[(int)char.MaxValue]; 
      // 1. 
      // Iterate over each character. 
      foreach (char t in sentence) 
      { 
       // Increment table. 
       upper[(int)t]++; 
      } 

      // 2. 
      // Write all letters found. 
      for (int i = 0; i < (int)char.MaxValue; i++) 
      { 
       if (upper[i] > 0 && 
       char.IsUpper((char)i)) 
       { 
        Console.WriteLine("Letter: {0} = {1}", 
         (char)i, 
         upper[i]); 
       } 
      } 
     } 

     public static void findSubString(string findString, string sentence) 
     { 
      int findStringCount = 0; 
      int startingPoint = 0; 


      if (findString.Length <= sentence.Length) 
      { 
       do 
       { 
        if (sentence.IndexOf(findString, startingPoint) != -1) 
        { 
         findStringCount++; 
         startingPoint = sentence.IndexOf(findString, startingPoint) + findString.Length; 
        } 

       } while (sentence.IndexOf(findString, startingPoint) != -1); 
      } 
      else 
      { 
       Console.WriteLine("Substring is too long!"); 
      } 
      Console.WriteLine("The number of times that {0} is found in the text is {1}", findString, findStringCount); 
     } 

    } 
} 
+0

這很好,但它不是一個數組,它只是一個循環。 :) – FhunderTuck

+0

@FhunderTuck只是一個循環? :) – MMK

0
using System; 
using System.Collections.Generic; 
using System.Text; 
using System.IO; 



namespace ConsoleApplication1 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 


     int[] counterArray = new int[123]; 
     string myString; 
     int wordCounted = 0; 
     int result = 0; 



     //Prompt user and get value 
     myString = getStringMethod(); 


     //Word count 
     wordCounted = countWordsMethod(myString, wordCounted); 




     //White space count 
     result = spaceCounterMethod(myString, result); 




     //Display words and space count, 
     displayCountMethod(counterArray, myString, wordCounted, result); 




     //Display uppercase letter count 
     displayUpperMethod(); 



     //Counting uppercase letter 
     uppserCaseMethod(counterArray); 

     //Display lowercase letter count 

     displayLowerMethod(); 




     //Counting lowercase letter 

     lowerCaseMethod(counterArray); 



     Console.WriteLine("Press ENTER to exit..."); 

     Console.ReadLine(); 
    } 

    private static void displayLowerMethod() 
    { 
     Console.WriteLine("LOWERCASE LETTERS: "); 
    } 

    private static void displayUpperMethod() 
    { 
     Console.WriteLine("\nUPPERCASE LETTERS: "); 
    } 

    private static void lowerCaseMethod(int[] counterArray) 
    { 
     for (int z = 97; z < 123; z++) 
     { 

      if (counterArray[z] > 0) 
      { 

       Console.WriteLine("\t\t{0}: \t{1}", Convert.ToChar(z),   counterArray[z]); 
      } 

     } 
    } 

    private static void upp 
+0

好,所以這是METHODS中的版本,顯然比使用數組更簡單。我得到了100%。 :D謝謝你的幫助。我用右鍵單擊和提取方法功能來做到這一點。 – FhunderTuck

相關問題