Friday, March 6, 2015

Count Vowels From text files in Debug Folder


using System.Windows.Forms;
using System.IO; //Add to work with fileSystem

namespace ZauBawk_CountVowelsFromTxtFiles
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnReadNCountVowelsFromTxtFiles_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            string[] files = Directory.GetFiles(@"txtFiles", "*.txt");

            StreamReader sr = null;
            try
            {
                Parallel.ForEach(files, currentFile =>
                {
                    sr = new StreamReader(currentFile);
                    int total = VowelCount(sr.ReadLine());
                    richTextBox1.AppendText("total vowel: " + total.ToString() + "\n");
                });
             
            }
            catch (AggregateException aggrEx)
            {

                foreach (Exception ex in aggrEx.InnerExceptions)
                {
                  MessageBox.Show(ex.Message);
                }
            }
        }

        //method to count vowels
        public static int VowelCount(String vowelName)
        {
            int counter = 0;
            char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
            for (int i = 0; i < vowelName.Length; i++)
            {
                if (vowels.Contains(vowelName[i]))
                {
                    counter++;
                }
            }
            return counter;
        }
    }
}

No comments:

Post a Comment