C# Count Vowels

后端 未结 17 1563
小鲜肉
小鲜肉 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:20

    That's because your if statement is always true, you need to compare the character at sentence[i], and see if it is a vowel, instead of seeing if the sentence contains a vowel.

    0 讨论(0)
  • 2020-12-03 09:20

    Application to count vowels and consonants letters in a sentence. This is another solution with less lines of code with understanding the idea of using loops and nested loops with char arrays.

    An application interface with control names:

    Application interface with controls names

    namespace Program8_4
    {
      public partial class Form1 : Form
      {
        // declare the counter variables in field
        int iNumberOfVowels = 0;
        int iNumberOfConsonants = 0;
        public Form1()
        {
            InitializeComponent();
        }
    
        private void btnFind_Click(object sender, EventArgs e)
        {
            // call the methods in this event
            GetVowels(txtStringInput.Text);
            GetConsonants(txtStringInput.Text);
            // show the result in a label
            lblOutput.Text = "The number of vowels : " + iNumberOfVowels.ToString()+ Environment.NewLine+
                "The number of consonants : " + iNumberOfConsonants.ToString();
            // assign zero the counters to not add the previous number to new number, and start counting from zero again
            iNumberOfVowels = 0;
            iNumberOfConsonants = 0;
    
        }
    
        private int GetConsonants(string strFindConsonants)
        {
            // Declare char array to contain consonants letters
            char[] chrConsonants = { 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'X',
                'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x' };
    
            // loop to get each letter from sentence
            foreach (char Consonants in strFindConsonants)
            {
            // another nested loop to compare each letter with all letters contains in chrConsonants array
                for (int index= 0; index<chrConsonants.Length;index++)
                {
                    // compare each letter with each element in charConsonants array
                    if (Consonants == chrConsonants[index])
    
                    {
                        // If it is true add one to the counter iNumberOfConsonants
                        iNumberOfConsonants++;
                  }
    
                }
            }
            // return the value of iNumberOfConsonants
            return iNumberOfConsonants;
        }
    
        private int GetVowels(string strFindVowels)
    
        {
            // Declare char array to contain vowels letters
            char[] chrVowels = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O','U' };
    
            // loop to get each letter from sentence
            foreach (char Vowels in strFindVowels)
            {
                // another nested loop to compare each letter with all letters contains in chrVowels array
                for (int index = 0; index< chrVowels.Length; index++)
                {
                    // compare each letter with each element in chrVowels array
                    if (Vowels == chrVowels[index])
    
                {
                        // If it is true add one to the counter iNumberOfVowels
                        iNumberOfVowels = iNumberOfVowels+1;
    
                }
                }
            }
            // return the value of iNumberOfVowels
            return iNumberOfVowels;
        }
    
    0 讨论(0)
  • 2020-12-03 09:21

    TMTOWTDI (Tim Toadie as they say: There's More Than One Way To Do It).

    How about

    static char[] vowels = "AEIOUaeiou".ToCharArray() ;
    public int VowelsInString( string s  )
    {
    
      int n = 0 ;
      for ( int i = 0 ; (i=s.IndexOfAny(vowels,i)) >= 0 ; )
      {
        ++n ;
      }
    
      return n;
    }
    

    Or (another regular expression approach)

    static readonly Regex rxVowels = new Regex( @"[^AEIOU]+" , RegexOptions.IgnoreCase ) ;
    public int VowelCount( string s )
    {
      int n = rxVowels.Replace(s,"").Length ;
      return n ;
    }
    

    The most straightforward is probably the fastest, as well:

    public int VowelCount( string s )
    {
      int n = 0 ;
      for ( int i = 0 ; i < s.Length ; +i )
      {
        switch( s[i] )
        {
        case 'A' : case 'a' :
        case 'E' : case 'e' :
        case 'I' : case 'i' :
        case 'O' : case 'o' :
        case 'U' : case 'u' :
          ++n ;
          break ;
        }
      }
      return n ;
    }
    
    0 讨论(0)
  • 2020-12-03 09:22

    // Using two loops.

            char[] vowels= new char[]{'a', 'e', 'i', 'o', 'u',
                                       'A', 'E', 'I', 'O', 'U'}; 
            string myWord= "This is a beautiful word.";
            
            int numVowels = 0;
            
            foreach(char c in  myWord.ToCharArray())
            {
                foreach(char c2 in vowels)
                {
                    if(c == c2) numVowels++;    
                }  
            }
            
            Console.WriteLine($"{numVowels} vowels in: {myWord}");
    
    0 讨论(0)
  • 2020-12-03 09:26

    this is a nice generic way to count vowels and from here you can do all sorts of things. count the vowels, return a sorted list, etc.

    public static int VowelCount(String vowelName) {
                int counter = 0;
                char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
                for (int index = 0; index < vowelName.Length; index++)
                {
                    if (vowels.Contains(vowelName[index])) 
                    {
                        counter++;
                    }
                }
                return counter;
            }
    
    0 讨论(0)
  • 2020-12-03 09:27

    Since Reed has answered your question, I will offer you another way to implement this. You can eliminate your loop by using LINQ and lambda expressions:

    string sentence = "The quick brown fox jumps over the lazy dog.";
    int vowelCount = sentence.Count(c => "aeiou".Contains(Char.ToLower(c)));
    

    If you don't understand this bit of code, I'd highly recommend looking up LINQ and Lambda Expressions in C#. There are many instances that you can make your code more concise by eliminating loops in this fashion.

    In essence, this code is saying "count every character in the sentence that is contained within the string "aeiou". "

    0 讨论(0)
提交回复
热议问题