C# 计算字符串中元音的数量
我有一个文本框,用户在其中输入一个随机字符串。我想计算字符串中元音(A、E、I、O、U)的数量,并在labelcontrol中显示结果C# 计算字符串中元音的数量,c#,asp.net,C#,Asp.net,我有一个文本框,用户在其中输入一个随机字符串。我想计算字符串中元音(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';
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();
}
您的代码中包含以下内容:
const string vowels = "aeiou";
return value.Count(chr => vowels.Contains(char.ToLower(chr)));
这是可行的,至少如果你的文化是我们的话。所以我不知道你为什么评论它支持现在的怪物
在上,它将失败,因为I
的小写不是I
,而是ı
(未启用)。因此,如果您将元音定义为aeiouAEIOU
,则应使用ToLowerInvariant
但是如果你想包含其他元音(比如Ä
),我不知道怎么做,除非列出所有字符
全面实施:
int CountVowels(string value)
{
const string vowels = "aeiou";
return value.Count(chr => vowels.Contains(char.ToLowerInvariant(chr)));
}
看起来您从以下方面获得了良好的代码部分:
int元音=0;
WriteLine(“请输入字符串:”);
字符串main=Console.ReadLine();
对于(int j=0;j
这可能有助于好运私有无效按钮1\u单击(对象发送者,事件参数e)
publicstaticvoidmain()
{
char[]句=新字符[100];
int i,元音=0,辅音=0,特殊=0,n;
Console.WriteLine(“输入句子长度\n”);
n=int.Parse(Console.ReadLine());
对于(i=0;i
#包括
使用名称空间std;
int main()
{
int t;
cin>>t;
集合元音={'A','E','I','O','U','A','E','I','O','U'};
而(t--)
{
字符串s;
int c=0;
cin>>s;
用于(字符i:s)
if(元音.find(i)!=元音.end())
++c;
cout字符串元音=(“aeiouAEIOU”);
int i=0;
int j=0;
int-vouelcount=0;
字符串main=textBox1.Text;/or Console.ReadLine();
而(j
你的大写字母加上stuckregex会更容易我在想你如何定义一个元音?只有英语中使用的5个元音,或者其他语言中也使用的元音?我知道没有哪个论坛认为使用大写字母写任何东西都是好的风格。让它成为超多元文化,使用ToLowerInvariant!这只涉及一个问题,但不涉及一些语言有额外的元音。而在其他语言中元音并不是适用于他们书面语言的概念(例如汉语不使用字母书写)@CodeInChaos嗨,我的应用程序中没有这些代码…我的代码运行良好,但我想最小化它意味着更少的代码..你能帮助我吗。这两行代码是OP中代码的一部分。你刚刚注释了它们。我是一个初学者,所以我正在学习如何有效地编写代码..我想用英语插入一个字符串..请帮助我学习!!!!试着解释一下你的代码是做什么的,以及它与问题中的代码有什么不同。这样大家学习起来容易多了。如果你发布几行关于你正在做什么的内容,而不是一个简单的代码块,这对OP很有帮助!
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();
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
{
string EnterString;
EnterString = textBox1.Text;
const string vowels = "aeiou";
label1.Text = EnterString.Count(myString => vowels.Contains(char.ToLowerInvariant(myString))).ToString();
}
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();
}
#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;
}
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
count = 0
string=input("Enter string:")
vowels = set("aeiouAEIOU")
for letter in string:
if letter in vowels:
count += 1
print("Count of the vowels is:")
print(count)