 |
| 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
|
|
|
|

April 23rd, 2005, 09:40 AM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
recursion
Modify the following code, rewrite the "CountWord()", use recursion.
using System;
public class StringUse
{
public static void Main()
{
string lines = "";
string[] words = {"the", "they", "them", "there"};
Console.WriteLine("Read 2 lines");
for (int i = 0; i < 2; ++i)
lines = string.Concat(lines, Console.ReadLine() + "\n");
lines = lines.ToLower();
Console.WriteLine("Read 2 lines");
Console.WriteLine("Echo 2 lines" + "\n" + lines);
for (int i = 0; i < words.Length; ++i)
Console.WriteLine(words[i] + " appears "
+ CountWords(lines, words[i]) + " times");
}
public static int CountWords(string s, string word)
{
string temp = s;
int i = s.IndexOf(word);
int count = 0;
while (i < temp.Length && i != -1)
{
Console.WriteLine("in while " + i);
++count;
temp = temp.Substring(i + word.Length,
temp.Length -i - word.Length);
i = temp.IndexOf(word);
}
return count;
}
}
|
|

April 24th, 2005, 02:58 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
What's this? Are you sharing useful code, or do you want us to do your homework?
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

April 24th, 2005, 06:42 AM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Please help me, I don`t understand how to do the recursion.
|
|

April 24th, 2005, 07:00 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
quote:do you want us to do your homework?
|
Do you?
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Everybody's Changing by Keane (Track 5 from the album: Hopes and Fears) What's This?
|
|

April 24th, 2005, 02:02 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
in recursion problems you should just find a recursion relation in your problem also you should have an initial check for stopping the recursion
Code:
public static int RecursiveCountWords(string s,string word)
{
int index=-1;
if (s==string.Empty) return 0;
index=s.IndexOf(word);
if(index!=-1)
s=s.Remove(index,word.Length);
else
return 0;
return 1+CountWordsRecursion(s,word);
}
get the idea?
_____________
Mehdi.
software student.
|
|

April 24th, 2005, 09:04 PM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes, I got the idea, thank you very much!!
|
|

April 28th, 2005, 09:36 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Mehdi- good job on his homework.
Hal Levy
I am here to help you, not do it for you.
That is, unless you hire me. I am looking for work.
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| xslt recursion (need help ) |
newbiexslt |
XSLT |
4 |
March 26th, 2007 10:59 AM |
| recursion in XSL |
jekkos |
XSLT |
8 |
January 2nd, 2007 06:16 PM |
| Recursion |
lincsimp |
XSLT |
1 |
August 16th, 2005 04:26 PM |
| Recursion |
shan9 |
JSP Basics |
0 |
November 17th, 2004 09:29 PM |
| recursion |
nulogix |
PHP How-To |
1 |
June 28th, 2004 03:58 PM |
|
 |