1

I have to create a small program where I have to prompt the user for idioms and store these into a text file. After that, I have to open up the text file and count the number of individual vowels in each idiom (a, e, i, o, u) and display these to the user.

Here is the code I have created so far:

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

3 回答 3

4

您可以使用以下代码获取字符串的元音计数:

int vowelCount = System.Text.RegularExpressions.Regex.Matches(input, "[aeoiu]").Count;

替换input为您的字符串变量。

如果您想计算大小写(大写/小写),您可以使用:

int vowelCount = System.Text.RegularExpressions.Regex.Matches(input.ToLower(), "[aeoiu]").Count;
于 2013-10-04T05:24:01.607 回答
1

string Target = "我的名字和你的名字未知 MY NAME AND YOUR NAME UNKNOWN";

列表模式=新列表{'a','e','i','o','u','A','E','I','O','U'};

int t = Target.Count(x => pattern.Contains(x));

于 2013-10-04T07:43:39.893 回答
0

我们可以使用正则表达式来匹配每个 idoms 中的元音。您可以调用下面提到的函数来获取元音计数。

工作代码片段:

  //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;
   }
于 2020-05-13T21:46:43.797 回答