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?
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
Quote:
Originally Posted by
grassclone
No loops allowed...have to do it recursive ly
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.
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.
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:
Code:
int find(String string, String pattern) {
int i= string.lastIndexOf(pattern);
if (i < 0) return 0;
return 1+find(string.substring(0, i));
}
warning: untested code.
kind regards,
Jos
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.
Re: finding number of times a substring appears in a string recursively
Quote:
Originally Posted by
Fubarable
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.
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;
}
}
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 :/
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.,
Code:
public static int myRecursiveMethod(SomeType someParameter) {
// .... some code
int valueReturned = myRecursiveMethod(somethingGoesHere);
// here use valueReturned some how.
}
Clear?
Re: finding number of times a substring appears in a string recursively
Quote:
Originally Posted by
Fubarable
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.,
Code:
public static int myRecursiveMethod(SomeType someParameter) {
// .... some code
int valueReturned = myRecursiveMethod(somethingGoesHere);
// here use valueReturned some how.
}
Clear?
How am I ignoring/disregarding my value if I'm returning it?? Really confused about that :( I looked at my code and still don't see how anything is being ignored :0
Re: finding number of times a substring appears in a string recursively
Code:
public static int countHello(String str, String findStr, int i) {
...
countHello(s1, s2, i - findStr.length());
...
}
Right there.
You do nothing with the value returned by countHello.
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,
Jim
Re: finding number of times a substring appears in a string recursively
Quote:
Originally Posted by
jim829
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,
Jim
Thanks