2012-05-22 221 views
-2

嗨,所以我有一些功课,目标是计算有多少单词,有空格,还有大写和小写字母。但令我困惑的是,输出必须显示每个字母A-Z a-z并在其旁边显示一个数字,以显示它出现的次数。在我的代码中,我真的不明白这一点。我只学这门课,所以我可以做思科网络,所以我不必做微积分。这个学期之后,我永远不会编程。并一直坚持了4个小时。任何帮助将不胜感激....即使你能帮助我理解如何做到这一点的概念?谢谢。C#计数每个大写字母和每个小写字母

我也曾经用这件事

using System; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int countedWords = 0, count = 0; //Declaring integers to be used 

      Console.WriteLine("Please enter a string\n"); 
      string inputString = Console.ReadLine(); 

      countedWords = inputString.Split(' ').Length; //counts words 
      Console.WriteLine("\nREPORT FOR: " + inputString + "\n"); 
      Console.WriteLine("WORDCOUNT: " + countedWords); 


      foreach (char c in inputString) //counts number of uppercase letters 
      { 
       if (Char.IsUpper(c)) 
        count++; 
      } 

      Console.WriteLine(count); 
      Console.ReadLine(); 
     } 
    } 
} 
+0

那么,这不完全是微积分。 :) – Allensb

+0

是啊,我不打算做任何与IT行业的C#相关的任何事情 – FhunderTuck

+0

我不是要求代码形式的答案。只是在LAYMANS条款中的一个解释,我可以通过一个n00b – FhunderTuck

回答

1

我会做的是创造的26(或52)个整数组成的阵列,它们全部初始化为0,每次你看到相应的字母,你增加适当的桶。你可以把它放到现有的foreach

对于显示,您只需遍历数组并打印它代表的字母以及该字母的实例数。

+0

我已经想到可能,但这么多的编码。有什么循环方法可以完成同样的事情吗? – FhunderTuck

+0

你已经有了必要的循环(你的'foreach')。你只需要在其中添加一个额外的比较/增量步骤。 –

2

由于这是作业 - 我会给你一个线索。

既然您想要对每个可能的字母进行计数,您可能需要考虑根据分组考虑此问题,而不是迭代和计数。这可以通过GroupBy来处理。

+0

我看着你的链接在那真的对我没有意义,让我再次说,我知道没有关于编程,我的老师是无用的,他被评为1/5的利率我的proff .com – FhunderTuck

1

做一个结构与 字母

  1. 字母
  2. 计数

建立清单出来,ListUpper,ListLower,ListSpace,ListOthers。 在您遇到元素时加载列表,并在完成后显示。你:)

+0

字面上,就像chineese对我 – FhunderTuck

+0

我的老师从未提及或向我们展示名单...功能... – FhunderTuck

+0

他们家伙教java和不断抱怨教学c# – FhunderTuck

0

这里是另一条线索

不会写代码:字符串中的每个人物都有一个数值,通常被称为它的ASCII值*。您可以使用str.ToCharArray()将字符串作为char的数组。

* 有关完整性的说明,比原始海报需求更详细。 C#中的字符串实际上存储为Unicode字符。但是,UTF-8的前128个字符映射到相应的ASCII字符,所以在这种情况下,差异并不重要。

+0

这篇文章可能会派上用场:http://stackoverflow.com/questions/4648781/how-to-get-character-for-a-given-ascii-value –

1

这是您的开始。

首先,我会把你的每一个声明放到一个新行中,并且使它们与它的用途有关(如果小写的计数器使得名为lowerCase_Count的变量等)IMO,因为它通常被认为是这是一个很好的练习,并且使得阅读代码和评论每个变量或对象所做的更容易。

接下来,如果我明白你写的是正确的,他正在寻找这个代码来显示每一个计数:大写字母,小写字母,空格(这只是我的空白或,:等?)和单词。他还希望列出每个字母在给定文本中出现的次数。

你有什么是一个好的开始,我增加了一些你的foreach循环,如下图所示:

using System; 
using System.IO; 
namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      //Declaring integers to be used 
      int countedWords = 0; 
      int cap_count = 0; 
      int lower_count = 0; 

      Console.WriteLine("Please enter a string\n"); 
      string inputString = Console.ReadLine(); 

      countedWords = inputString.Split(' ').Length; //counts words 
      Console.WriteLine("\nREPORT FOR: " + inputString + "\n"); 
      Console.WriteLine("WORDCOUNT: " + countedWords); 


      foreach (char c in inputString) 
      { 
       //if is upper case add to cap_count 
       if (Char.IsUpper(c)) 
        cap_count++; 
       //if char is a punctuation or white space ignore it else 
       // add as lower case 
       else if (!char.IsPunctuation(c) && !char.IsWhiteSpace(c)) 
        lower_count++; 

      } 

      //display results. 
      Console.WriteLine("Number of Letters: " + (cap_count + lower_count)); 
      Console.WriteLine("Number of capital Letters: " + cap_count); 
      Console.WriteLine("Number of lower case Letters: " + lower_count); 
      Console.WriteLine("Number of spaces: " + (countedWords - 1)); 


      Console.ReadLine(); 
     } 
    } 
} 

这应该是一个很好的起点为你完成其余部分。我希望这有帮助。

+0

不会逗号使'lower_count'增量,因为他们既不是Upper也不是分隔符?我不是C#的人,但只是问。 –

+0

啊好抓。我没有测试过,但上面的代码应该可以工作。 – James213

+0

非常感谢你,我现在在学校,正在看看我能否理解你做了什么。感谢您的每一个输入。 – FhunderTuck

-8
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(); 

     } 
    } 
} 
+4

-1。最差的。可能。解。几乎没有办法选择不太有效的方法。 –

+3

您是按照每行代码付款吗? – Servy

相关问题