Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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
 
Old October 2nd, 2008, 07:49 PM
Registered User
 
Join Date: Sep 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

 
Old October 2nd, 2008, 08:18 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

string sa = new string[] { textbox2.Text )
int howMany = textbox1.Text.Split( sa, StringSplitOptions.None ).Length - 1
 
Old October 2nd, 2008, 08:54 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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
 
Old October 2nd, 2008, 09:01 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

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"/>
 
Old October 3rd, 2008, 07:58 AM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default

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)
 
Old October 3rd, 2008, 01:55 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

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.
 
Old October 3rd, 2008, 02:31 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
 
Old October 3rd, 2008, 02:58 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

ROTFLMAO.

Now, the question is, *which* "OP" was Imar referring to?

After all, my initials are "OP".

<snicker/>
 
Old October 3rd, 2008, 03:07 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

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.
 
Old October 3rd, 2008, 03:39 PM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default

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)





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to count the number of access the webpage ? khb3283 ASP.NET 2.0 Basics 2 October 2nd, 2008 09:08 PM
Need to Count number of lines in a file lawsoncobol Access VBA 2 July 19th, 2007 04:28 AM
Count the number of output lines joyces XSLT 7 July 6th, 2005 11:49 AM
count the number of rows in the table crmpicco Javascript How-To 4 February 2nd, 2005 12:58 AM
count the TOTAL number of segments crmpicco Classic ASP Basics 2 February 1st, 2005 05:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.