Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 2nd, 2008, 07:49 PM
Registered User
 
Join Date: Sep 2008
Location: Ho Chi Minh, , Vietnam.
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

Reply With Quote
  #2 (permalink)  
Old October 2nd, 2008, 08:18 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
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
Reply With Quote
  #3 (permalink)  
Old October 2nd, 2008, 08:54 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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
Reply With Quote
  #4 (permalink)  
Old October 2nd, 2008, 09:01 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
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"/>
Reply With Quote
  #5 (permalink)  
Old October 3rd, 2008, 07:58 AM
Friend of Wrox
Points: 1,166, Level: 13
Points: 1,166, Level: 13 Points: 1,166, Level: 13 Points: 1,166, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
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)
Reply With Quote
  #6 (permalink)  
Old October 3rd, 2008, 01:55 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
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.
Reply With Quote
  #7 (permalink)  
Old October 3rd, 2008, 02:31 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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.
Reply With Quote
  #8 (permalink)  
Old October 3rd, 2008, 02:58 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
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/>
Reply With Quote
  #9 (permalink)  
Old October 3rd, 2008, 03:07 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
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.
Reply With Quote
  #10 (permalink)  
Old October 3rd, 2008, 03:39 PM
Friend of Wrox
Points: 1,166, Level: 13
Points: 1,166, Level: 13 Points: 1,166, Level: 13 Points: 1,166, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
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)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:33 PM.


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