2011-06-13 42 views
-4

我有一個用戶輸入隨機字符串的文本框。我想計算字符串中元音的數量(A,E,I,O,U),並在labelcontrol中顯示結果。計算字符串中元音的數量

protected void Button1_Click(object sender, EventArgs e) 
    { 
     string EnterString; 
     EnterString = TextBox1.Text; 
     char ch1 = 'a'; 
     char ch2 = 'e'; 
     char ch3 = 'i'; 
     char ch4 = 'o'; 
     char ch5 = 'u'; 

     int counta = 0; 
     int counte = 0; 
     int counti = 0; 
     int counto = 0; 
     int countu = 0; 
     char ch6 = 'A'; 
     char ch7 = 'E'; 
     char ch8 = 'I'; 
     char ch9 = 'O'; 
     char ch10 = 'U'; 

     int countA = 0; 
     int countE = 0; 
     int countI = 0; 
     int countO = 0; 
     int countU = 0; 

     //const string vowels = "aeiou"; 
     /* return value.Count(chr => vowels.Contains(char.ToLower(chr))); 
     return Value.Count()*/ 
     int j = counta + counte + counti + counto + countu + countA + countE + countI + countO + countU; 

     foreach (char v in EnterString) 
     { 
      if (v == ch1) { counta++; j++; } 

      else if (v == ch2) { counte++; j++; } 

      else if (v == ch3) { counti++; j++; } 

      else if (v == ch4) { counto++; j++; } 

      else if (v == ch5) { countu++; j++; } 
     } 
     foreach (char v in EnterString) 
     { 
      if (v == ch6) { countA++; j++; } 

      else if (v == ch7) { countE++; j++; } 

      else if (v == ch8) { countI++; j++; } 

      else if (v == ch9) { countO++; j++; } 

      else if (v == ch10) { countU++; j++; } 
     } 

     Label1.Text = j.ToString(); 
    } 
+6

你的大寫鎖定被困 – CodesInChaos 2011-06-13 14:44:59

+1

正則表達式會更容易些我在想 – caesay 2011-06-13 14:45:09

+0

爲什麼要對我大吼大叫? – zeroef 2011-06-13 14:45:12

回答

4

您在代碼中有這樣的:

const string vowels = "aeiou"; 
return value.Count(chr => vowels.Contains(char.ToLower(chr))); 

這樣的作品,至少,如果你的文化是美國。所以不知道爲什麼你評論它,贊成目前的怪物。

在一個Turkish locale它會失敗,因爲I較低的情況下是不是iı(undotted)。因此,如果您將元音定義爲aeiouAEIOU,則應使用ToLowerInvariant

但是,如果你想包括其他元音(如Ä)我不知道如何做到這一點,除了列出所有的字符。

全面實施:

int CountVowels(string value) 
{ 
    const string vowels = "aeiou"; 
    return value.Count(chr => vowels.Contains(char.ToLowerInvariant(chr))); 
} 

看起來你得到了良好的代碼部分: Counting vowels using switch

+1

讓它超多元文化並使用ToLowerInvariant! – ThePower 2011-06-13 14:53:40

+0

這涉及一個問題,但不涉及一些語言具有額外元音的問題。而在其他語言中,元音不適用於他們的書面語言(例如,中文不使用基於字母的書寫) – CodesInChaos 2011-06-13 14:56:14

+0

@CodeInChaos您好,我沒有在我的應用程序中使用這些代碼...我的代碼很好,但我想要儘量減少它意味着更少的代碼..你能幫助我。 – 2011-06-13 14:58:47

2
int vowel = 0; 
     Console.WriteLine("Please enter the string:"); 
     string main = Console.ReadLine(); 
     for (int j = 0; j < main.Length ; j++) 
     { 
      if (main[j] == 'a' || main[j] == 'A' || main[j] == 'e' || main[j] == 'E' || main[j] == 'i' || main[j] == 'I' || main[j] == 'o' || main[j] == 'O' || main[j] == 'u' || main[j] == 'U') 
      { 
       vowel++; 
      } 
     } 
     Console.WriteLine("Number of vowels in sentence is :"+ vowel); 
     Console.ReadLine(); 
+1

嘗試解釋你的代碼的作用,以及它與問題中的代碼有什麼不同。每個人都可以更容易地學習這種方式。 – 2012-09-17 14:50:29

2
Console.WriteLine("Please input your text: ") 
    mystring = Console.ReadLine 
    For i = 1 To mystring.Length 
     Console.Write((mystring(i - 1)) & ",") 
     isItAVowel = False 
     If mystring(i - 1) = "a" Or mystring(i - 1) = "A" Then isItAVowel = True 
     If mystring(i - 1) = "e" Or mystring(i - 1) = "E" Then isItAVowel = True 
     If mystring(i - 1) = "i" Or mystring(i - 1) = "I" Then isItAVowel = True 
     If mystring(i - 1) = "o" Or mystring(i - 1) = "O" Then isItAVowel = True 
     If mystring(i - 1) = "u" Or mystring(i - 1) = "U" Then isItAVowel = True 
     If isItAVowel Then 

這可以幫助好運

0

私人無效的button1_Click(對象發件人,EventArgs e)

{ 

     string EnterString; 
     EnterString = textBox1.Text; 
     const string vowels = "aeiou"; 
     label1.Text = EnterString.Count(myString => vowels.Contains(char.ToLowerInvariant(myString))).ToString(); 

    } 
0
public static void Main() 
{ 
    char[] sentence = new char[100]; 

    int i, vowels = 0, consonants = 0, special = 0, n; 
    Console.WriteLine("Enter the Length of the sentence \n"); 
    n = int.Parse(Console.ReadLine()); 
    for (i = 0; i < n; i++) 
    { 
     sentence[i] = Convert.ToChar(Console.Read()); 
    } 
    for (i = 0; sentence[i] != '\0'; i++) 
    { 
     if ((sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] == 
     'i' || sentence[i] == 'o' || sentence[i] == 'u') || 
     (sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] == 
     'I' || sentence[i] == 'O' || sentence[i] == 'U')) 
     { 
      vowels = vowels + 1; 
     } 
     else 
     { 
      consonants = consonants + 1; 
     } 
     if (sentence[i] == 't' || sentence[i] == '\0' || sentence[i] == ' ') 
     { 
      special = special + 1; 
     } 
    } 

    consonants = consonants - special; 
    Console.WriteLine("No. of vowels {0}", vowels); 
    Console.WriteLine("No. of consonants {0}", consonants); 
    Console.ReadLine(); 
    Console.ReadLine(); 
} 
0
#include <bits/stdc++.h>  
using namespace std;  
int main()  
{  
    int t; 
    cin >> t; 
    set<char> vowels = {'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'}; 
    while(t--) 
    { 
     string s; 
     int c = 0; 
     cin >> s; 
     for(char i : s) 
      if(vowels.find(i) != vowels.end()) 
       ++c; 
     cout << c << "\n"; 
    } 
    return 0; 
} 
0
 string vowelList = ("aeiouAEIOU"); 
     int i = 0; 
     int j = 0; 
     int vowelCount = 0; 

     string main = textBox1.Text; /or Console.ReadLine(); 

     while (j < main.Length) 

     { 
      while (i < vowelList.Length) 
      { 
       if (main[j] == vowelList[i]) 
       { 
        vowelCount++; 
       } 

       i++; 
      } 

      j = j + 1; 
      i = 0; 
     } 

     label1.Text = ("Number of vowels in sentence is :" + vowelCount); /or Console.Writeline ("Number of vowels in sentence is: " + vowelCount 
相關問題