 |
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

October 2nd, 2008, 07:49 PM
|
Registered User
|
|
Join Date: Sep 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
How to count the number of occurence of a string ?
I have 2 textbox and 1 button .Assume that I input in textbox 1 "aaa gdds aaa dfdfdfgggg aaa" and in textbox 2 "aaa".When I click the button, there will be a message
The number of occurence string "aaa" in textbox 1 is : 3
Could you give me the code by c#
Thank
|

October 2nd, 2008, 08:18 PM
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
string sa = new string[] { textbox2.Text )
int howMany = textbox1.Text.Split( sa, StringSplitOptions.None ).Length - 1
|

October 2nd, 2008, 08:54 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
khb3283,
This sounds like a homework assignment.
What about if the value of textbox 1 is "aaaaaaaaaaaa"?
Should the answer be 0, 4 or 10?
-Peter
compiledthoughts.com
|

October 2nd, 2008, 09:01 PM
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
Yeah, it's surely homework. I was just too lazy to chide him/her for not at least trying. Shame on me.
I like your question!
What about if the answer should be 1? <grin style="evil"/>
|

October 3rd, 2008, 07:58 AM
|
Friend of Wrox
|
|
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
|
|
My vote is for 0 occurences...
Dr. Purdum
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
|

October 3rd, 2008, 01:55 PM
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
I don't think we get to vote. I think the instructor who assigned that homework is in control.
In real world usage, though, I think the answer should be either zero or one.
"Find how many times a given word [including inflections] appears in a given string."
If the [including inflections] is in there, then 1 (e.g., "ants" vs. "ant", "singing" vs. "sing"). If not, then 0.
(Sort of. Yes, I know, "there" is NOT an inflection of "the". And "sang" is an inflection of "sing." But if you want to get that nit-picky, then we'll have to incorporate an English-language dictionary in the answer.)
Aren't we having fun with poor khb3283?
**********
p.s.: A friend of mine wants to know: When are you going to write a VB version of that book? She's willing to tackle it as is, but her "shop" uses VB.NET exclusively. She's a competent ASP developer (VBScript, JavaScript, HTML, SQL) but is drowning in OO.
|

October 3rd, 2008, 02:31 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Why are you teasing the OP so much instead of helping him? The answer is of course extremely easy:
protected void Button1_Click(object sender, EventArgs e)
{
MessageBox.Show("The number of occurence string \"aaa\" in textbox 1 is : 3");
}
And lot easier if you ask me, and removes the need for any counting at all.
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
|

October 3rd, 2008, 02:58 PM
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
ROTFLMAO.
Now, the question is, *which* "OP" was Imar referring to?
After all, my initials are "OP".
<snicker/>
|

October 3rd, 2008, 03:07 PM
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
KHB3283:
You know "planoie" posted the only truly correct answer here, so far.
That is, you haven't defined the problem well enough for anybody to give a definitive answer. Now, this is quite possibly because your instructor didn't give you a good and full problem description.
If so, show you are smart and go back and ask him or her Planoie's question. Rephrasing it, it would read:
What about if the value of textbox 1 is "aaaaaaaaaaaa" and textbox 2 is "aaa"?
Should the answer be 0, 1, 4 or 10? Or what?
*ALL* of those answers could be correct, depending on how the instructor wants to define the problem.
|

October 3rd, 2008, 03:39 PM
|
Friend of Wrox
|
|
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
|
|
I misread the second post...my bad. Also, I would think one should increment an index so the second compare is past the first match. (I'm pretty sure the Boyer-Moore algorithm works this way.) In that case, I would think that, if the question is:
What about if the value of textbox 1 is "aaaaaaaaaaaa" and textbox 2 is "aaa"?
the answer should be 4.
Dr. Purdum
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
|
|
 |