我必须创建一个小程序,在这个程序中,我必须提示用户输入习惯用法,并将其存储到文本文件中。在此之后,我必须打开文本文件,计算每个习惯用法(a,e,i,o,u)中的单个元音的数量,并将这些元音显示给用户。
以下是我到目前为止创建的代码:
int numberOfIdioms;
string fileName = "idioms.txt";
int countA = 0, countE = 0, countI = 0, countO = 0, countU = 0;
Console.Title = "String Functions";
Console.Write("Please enter number of idioms: ");
numberOfIdioms = int.Parse(Console.ReadLine());
string[] idioms = new string[numberOfIdioms];
Console.WriteLine();
for (int aa = 0; aa < idioms.Length; aa++)
{
Console.Write("Enter idiom {0}: ", aa + 1);
idioms[aa] = Console.ReadLine();
}
StreamWriter myIdiomsFile = new StreamWriter(fileName);
for (int a = 0; a < numberOfIdioms; a++)
{
myIdiomsFile.WriteLine("{0}", idioms[a]);
}
myIdiomsFile.Close();发布于 2013-10-04 13:24:02
您可以使用以下代码来获取字符串的元音计数:
int vowelCount = System.Text.RegularExpressions.Regex.Matches(input, "[aeoiu]").Count;将input替换为您的字符串变量。
如果您希望不区分大小写(大写/小写)进行计数,则可以使用:
int vowelCount = System.Text.RegularExpressions.Regex.Matches(input.ToLower(), "[aeoiu]").Count;发布于 2013-10-04 15:43:40
string Target =“我的名字和你的名字未知我的名字和你的名字未知”;
List pattern = new List { 'a','e','i','o','u','A','E','I','O','U‘};
int t= Target.Count(x => pattern.Contains(x));
发布于 2020-05-14 05:46:44
我们可以使用正则表达式来匹配每个idom中的元音。您可以调用下面提到的函数来获取元音计数。
工作代码片段:
//below function will return the count of vowels in each idoms(input)
public static int GetVowelCount(string idoms)
{
string pattern = @"[aeiouAEIOU]+"; //regular expression to match vowels
Regex rgx = new Regex(pattern);
return rgx.Matches(idoms).Count;
}https://stackoverflow.com/questions/19173654
复制相似问题