2016年10月13日 星期四

#00494_Kindergarten Counting Game

分級:1
分類:字元與字串

題目:

Everybody sit down in a circle. Ok. Listen to me carefully.
 “Woooooo, you scwewy wabbit!”
Now, could someone tell me how many words I just said?

Input 
Input to your program will consist of a series of lines, each line containing multiple words (at least one). A “word” is defined as a consecutive sequence of letters (upper and/or lower case).

Output 
Your program should output a word count for each line of input. Each word count should be printed on a separate line.

Sample Input
Meep Meep!
 I tot I taw a putty tat. I did! I did!
 I did taw a putty tat.
 Shsssssssssh ... I am hunting wabbits. Heh Heh Heh Heh ...

 Sample Output 
2
7
10
9




using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CPE_test
{
    public partial class CountWord : Form
    {
        public CountWord()
        {
            InitializeComponent();
            this.btnSumit.Click += new EventHandler(btnSumit_Click);
        }
        private void btnSumit_Click(object sender, EventArgs e)
        {
            string sOrigin = this.txtInput.Text;
            string[] sSpilebyRow = sOrigin.Split(new string[] {"\n"}, StringSplitOptions.RemoveEmptyEntries);
            int iCountWord = 0;
            for (int intA = 0; intA < sSpilebyRow.Length; intA++)
            {
                iCountWord = 0;
                string[] snRow = sSpilebyRow[intA].Split(' ');
                for (int intB = 0; intB < snRow.Length; intB++)
                {
                    if(judgword(snRow[intB]))iCountWord++;
                }
                this.txtOutput.Text += iCountWord + Environment.NewLine;
            }
        }
        /// </code><br />
        <summary><code>用來判斷字還是...</code></summary><code>
        public bool judgword(string sMsg)
        {
            int iNum = 0;
            char[] stamp = sMsg.ToCharArray();
            for (int intA = 0; intA &lt; sMsg.Length; intA++)
            {
                if ((stamp[intA] &gt;= 'a' &amp;&amp; stamp[intA] &lt;= 'z') || (stamp[intA] &gt;= 'A' &amp;&amp; stamp[intA] &lt;= 'Z'))
                {
                    iNum++;
                }
            }
            if (iNum &gt; 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

沒有留言:

張貼留言