Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
|
BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
This is the forum to discuss the Wrox book Beginning C# 3.0 : An Introduction to Object Oriented Programming by Jack Purdum; ISBN: 9780470261293
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 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 April 26th, 2011, 06:31 PM
Authorized User
 
Join Date: Feb 2011
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
Default Chapter 5 code modification. Counting the number of times a certain string occurs.

Morning, ive been working through all the examples upto chapter 5 and i decided to try and modify q3 in the exercises by trying to determine the number of times a string ie "here" was present in a larger string.

Unfortunately i seem to have bitten off more than i can chew. What i have atm, which doesnt work, is as follows.

Code:
    private void button1_Click(object sender, EventArgs e)
    {
        string temp;
        String tempx;
        int number123;
        int count;
        string wordError;
        int x;
        bool flag;

        wordError = txtError.Text;
        wordCorrect = txtCorrect.Text;
        count = 0;



        tempx = txtInput.Text;
        temp = txtInput.Text.Length.ToString();

        flag = int.TryParse(temp, out number123);
        if (flag == false)
        {
            MessageBox.Show("Text data only");
            txtInput.Focus();
            return;
        }

        for (x = 0; x <= number123; x++)
        {

            if (tempx.Equals(wordError))
            {
                count += 1;
            }
        }

        txtResult.Text =count.ToString();
   }
}
if someone could comment as to how i should be doing this i would appreciate it. (Im assuming the above code is comparing the whole string to the wordError variable??)

Thanks in advance

Last edited by scolty; April 26th, 2011 at 06:33 PM..
 
Old April 28th, 2011, 12:00 AM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default String pattern match

I wrote an article on this some time ago, mainly to show how minor algorithm changes lowered the pattern search time from about 45 seconds to just over 3 seconds. (Computers were A LOT slower back then!) See: "Pattern Matching in C", Computer Language, Nov., 1987. The changes would be easy to rewrite in C#. I'm out of town right now, but will try to dig out the article when I get home.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
 
Old April 28th, 2011, 06:52 AM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default Quick attempt

I thought I'd give you something to work with until I get home. Try creating two textboxes, one for the input string to search and one for the pattern. Create a third textbox to show the number of matches found. Now try:

Code:
privatevoid btnSearch_Click(object sender, EventArgs e)
{
int matchCounter = 0;
int i;
int inputLength;
int patternLength;
string buffer = txtInputString.Text.ToLower(); // Make lower case
string target = txtTargetPattern.Text.ToLower();
 
inputLength = buffer.Length;             // Get string lengths
patternLength = target.Length;
 
i = 0;
while (i < inputLength)
{
 i = buffer.IndexOf(target, i);
 
 if (i == -1)         // We're done...
    break;
 matchCounter++;      //...up the counter...
 i += patternLength;  //...and go to the end of the pattern
}                      // End while (i...
 
txtCount.Text = matchCounter.ToString();
}
First we convert both strings to lower case. You could add code to preserve case based on the state of a checkbox indicating the user wants the case preserved. I'll leave that to you (as well as error checking). Next, we get the lengths of the two strings, using the variable i and patterLength to control the while loop. The method IndexOf() returns an index telling where a match on the pattern is found. If no (further) match is found, the index returned is -1. Note that we use that to break out of the loop. If we do find a match, we record the match in matchCounter and then advance past the match to look for additional matches.

I think if you single-step through the code, you'll see how it works.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
 
Old April 28th, 2011, 08:29 AM
Authorized User
 
Join Date: Feb 2011
Posts: 21
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Afternoon Dr Purdum,

Thanks very much for the sample code. Ive had a read through it and i think i understand how it works. I will experiment with it tonight after work + attempt to implement your suggest homework exercise.

Regarding your article on "Pattern Matching in C", if it doesnt require a large amount of digging around then id be interesting in having a look. Ill send you a PM with my email address. (I see u dont have an email address so mine is [email protected])

Cheers again for your help, much appreciated.

Rgds

Scolty.

Last edited by scolty; April 28th, 2011 at 09:38 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
for-each loop predefined number of times vonkeldoos XSLT 1 February 22nd, 2007 07:47 AM
Counting Number of Rows Between Data Range eusanpe Excel VBA 6 September 21st, 2006 07:17 AM
Wrox Blog Modification - Chapter 6 dougydoe BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 8 August 13th, 2006 06:49 PM
Counting number of spaces in a line Suomi Access VBA 3 September 9th, 2005 02:34 PM
Prime Number Counting. Help!! ajm235 C++ Programming 3 August 27th, 2004 12:00 PM





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