C# Count Vowels

后端 未结 17 1564
小鲜肉
小鲜肉 2020-12-03 08:59

I am learning to program C# and I am trying to count the vowels. I am getting the program to loop through the sentence, but instead of returning vowel count, it is just retu

相关标签:
17条回答
  • 2020-12-03 09:39

    You were checking to see if your whole sentence contained vowels for every iteration of your loop, which is why your total was simply the number of characters in your sentence string.

    foreach(char ch in sentence.ToLower())
        if("aeiou".Contains(ch))
            total++;
    

    Better yet use a regular expression. edit You'd only want to use a regex for something a little more complex than matching vowels.

    using System.Text.RegularExpressions;
    ...
    int total = Regex.Matches(sentence, @"[AEIOUaeiou]").Count;
    

    EDIT Just for completeness the fastest/most efficient (if you were to do this on a ~million strings) solution. If performance wasn't a concern I'd use Linq for its brevity.

    public static HashSet<char> SVowels = new HashSet<char>{'a', 'e', 'i', 'o', 'u'};
    public static int VowelsFor(string s) {
        int total = 0;
        foreach(char c in s)
            if(SVowels.Contains(c))
                total++;
        return total;
    }
    
    0 讨论(0)
  • 2020-12-03 09:39

    There are many ways to skin a cat :-) In programming a little lateral thinking can be useful...

    total += sentence.Length - sentence.Replace("a", "").Length;
    total += sentence.Length - sentence.Replace("e", "").Length;
    total += sentence.Length - sentence.Replace("i", "").Length;
    total += sentence.Length - sentence.Replace("o", "").Length;
    total += sentence.Length - sentence.Replace("u", "").Length;
    

    You could, for example, try removing a vowel from the sentence and looking if the sentence is smaller without the vowel, and by how much.

    0 讨论(0)
  • void main()
    {
        int x=0;
        char ch;
        printf("enter a statement:");
        while((ch=getche())='\r')
        {
            if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
            x++;
        }
        printf("total vowels=");
        getch();
    }
    
    0 讨论(0)
  • 2020-12-03 09:40

    Maybe too advanced for a starter, but this is the way you do that in C#:

    var vowels = new[] {'a','e','i','o','u'};
    
    Console.WriteLine("Enter a Sentence");
    var sentence = Console.ReadLine().ToLower();
    
    var vowelcount = sentence.Count(x => vowels.Contains(x));
    
    Console.WriteLine("Your total number of vowels is: {0}", vowelcount);
    Console.ReadLine();
    
    0 讨论(0)
  • 2020-12-03 09:43

    This is how I would handle this.

    var sentence = "Hello my good friend";
                var sb = sentence.ToLower().ToCharArray();
                var count = 0;
                foreach (var character in sb)
                {
                    if (character.Equals('a') || character.Equals('e') || character.Equals('i') || character.Equals('o') ||
                        character.Equals('u'))
                    {
                        count++;
                    }
                }
    
    0 讨论(0)
提交回复
热议问题