Results 1 to 14 of 14
- 02-11-2013, 02:41 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 5
- Rep Power
- 0
finding number of times a substring appears in a string recursively
public class JavaApplication7 {
public static int countHello(String str, String findStr, int i) {
String s1 = str;
String s2 = findStr;
int count = 0;
if (i > 0) {
i = s1.lastIndexOf(s2, i);
countHello(s1, s2, i - findStr.length());
count++;
System.out.println(count);
return count;
} else {
return 0;
}
}
public static void main(String[] args) {
String str = "Java*is a*programming language*originally developed by*James Gosling*at*Sun Microsystems* and released in 1995 as a core component of Sun Microsystems*Java platform. The language derives much of its*syntax*from*C*and*C++*but has a simpler*object model*and fewer*low-level facilities. Java applications are typically*compiled*to*bytecode*(class file) that can run on any*Java Virtual Machine*(JVM) regardless of*computer architecture. Java is a general-purpose, concurrent, class-based, object-oriented language that is specifically designed to have as few implementation dependencies as possible. Java is currently one of the most popular programming languages in use, particularly for client-server web applications, with a reported 10 million users.";
String findStr = "the";
JavaApplication7.countHello(str.toLowerCase(), findStr, str.length());
}
}
The output I get is 0, 0, 0. Which is really weird considering there's only two "the"s and on top of that count never gets incremented...Any ideas??
-
Re: finding number of times a substring appears in a string recursively
For the count to increment usually have to use a loop such as a for loop or a while loop. Do you use a loop in your code?
- 02-11-2013, 02:50 AM #3
Member
- Join Date
- Feb 2013
- Posts
- 5
- Rep Power
- 0
Re: finding number of times a substring appears in a string recursively
No loops allowed...have to do it recursive ly
-
Re: finding number of times a substring appears in a string recursively
That would be pretty important information that I would have assumed you'd place in your original question, no? Next time, please give all pertinent information, and please use code tags so we can more easily read your code (see the links in my signature below).
OK, well knowing that, then it looks like you're not using recursion correctly. Your recursing method returns an int, and so ask yourself, are you using the int returned from the method when you call it recursively? Perhaps if you use that number you can get your count variable to increase.
- 02-11-2013, 07:14 AM #5
Member
- Join Date
- Sep 2012
- Posts
- 1
- Rep Power
- 0
Re: finding number of times a substring appears in a string recursively
The variable count should be a class variable as its getting updated by recursive function call.
- 02-11-2013, 07:44 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Re: finding number of times a substring appears in a string recursively
You are discarding the return value of the recursive method call (it returns the number of hits in the rest of the String to search). Your method is basically correct except for the previous remark. Something like this will do:
warning: untested code.Java Code:int find(String string, String pattern) { int i= string.lastIndexOf(pattern); if (i < 0) return 0; return 1+find(string.substring(0, i)); }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
-
Re: finding number of times a substring appears in a string recursively
My point exactly. You're throwing out the value being returned by the recursive method call rather than assigning it to something.
- 02-12-2013, 01:13 AM #8
Member
- Join Date
- Feb 2013
- Posts
- 5
- Rep Power
- 0
Re: finding number of times a substring appears in a string recursively
Okay I'm still getting an hit on count...There's only two "the" statements in my large string. Count is returning 3 instead of 2...and I can't figure out a legit solution :/PHP Code:public class Recursive { static int count = 0; public static int countHello(String str, String findStr, int i) { String s1 = str; String s2 = findStr; if (i > 0) { i = s1.lastIndexOf(s2, i); countHello(s1, s2, i - findStr.length()); return count++; } else { return 0; } }
- 02-12-2013, 01:15 AM #9
Member
- Join Date
- Feb 2013
- Posts
- 5
- Rep Power
- 0
Re: finding number of times a substring appears in a string recursively
The last index of "the" of the string I posted is 622...so that's why it's counting an extra time...any suggestions or do i need to not use last index of...?
-
Re: finding number of times a substring appears in a string recursively
Again, the countHello method returns something. Again, you're ignoring its return value. I'm not sure how to spell it out further other than you need to call countHello on the left hand side of an assignment statement and have something on the right that will accept what is returned. We've been suggesting this to you over and over as it is key to your solving your problem.
i.e.,
Clear?Java Code:public static int myRecursiveMethod(SomeType someParameter) { // .... some code int valueReturned = myRecursiveMethod(somethingGoesHere); // here use valueReturned some how. }
- 02-19-2013, 03:33 PM #11
Member
- Join Date
- Feb 2013
- Posts
- 5
- Rep Power
- 0
- 02-19-2013, 04:22 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: finding number of times a substring appears in a string recursively
Right there.Java Code:public static int countHello(String str, String findStr, int i) { ... countHello(s1, s2, i - findStr.length()); ... }
You do nothing with the value returned by countHello.Please do not ask for code as refusal often offends.
- 02-20-2013, 02:57 AM #13
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 693
- Rep Power
- 1
Re: finding number of times a substring appears in a string recursively
Hope you folks don't mind if I jump in. When you make a recursive call you ultimately need to pass the result back up the stack via returns. So the question is, what is it you are returning? It is something that you have calculated by passing updated values of it in subsequent recursive calls. When the recursive call ends because of some check, then the value is returned back up the call stack and ultimately to the first invocation of the method.
Also, you don't need to make a reference to any global (e.g. instance or static) variables. Only local variables and those that are passed as arguments to the recursive method call.
Regards,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 02-24-2013, 01:04 PM #14
Member
- Join Date
- Feb 2013
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Counting the length of a string recursively
By Zoralink23 in forum New To JavaReplies: 1Last Post: 11-29-2012, 03:37 AM -
Recursively searching a string
By fam2315 in forum New To JavaReplies: 0Last Post: 10-13-2012, 06:25 PM -
Dice help. posting the number of times a number is rolled.
By cookiejarvus in forum New To JavaReplies: 13Last Post: 12-04-2011, 11:08 PM -
Printing the Number of Times a Number in a Range Shows up
By space4rent00 in forum New To JavaReplies: 1Last Post: 02-05-2010, 10:42 PM -
inputs numbers then outputs how many time a particular number appears
By koji_kun in forum New To JavaReplies: 23Last Post: 12-22-2009, 08:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks