分類:字元與字串
題目:
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 < sMsg.Length; intA++)
{
if ((stamp[intA] >= 'a' && stamp[intA] <= 'z') || (stamp[intA] >= 'A' && stamp[intA] <= 'Z'))
{
iNum++;
}
}
if (iNum > 0)
{
return true;
}
else
{
return false;
}
}
}
}
沒有留言:
張貼留言