Results 1 to 14 of 14
  1. #1
    grassclone is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

  3. #3
    grassclone is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: finding number of times a substring appears in a string recursively

    No loops allowed...have to do it recursive ly

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: finding number of times a substring appears in a string recursively

    Quote Originally Posted by grassclone View Post
    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.

  5. #5
    krjavatech is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

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

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

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

    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));
    }
    warning: untested code.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

  8. #8
    grassclone is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: finding number of times a substring appears in a string recursively

    Quote Originally Posted by Fubarable View Post
    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.
    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;
        } 
    }
    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 :/

  9. #9
    grassclone is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default 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...?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.,

    Java Code:
    public static int myRecursiveMethod(SomeType someParameter) {
      // .... some code
      int valueReturned = myRecursiveMethod(somethingGoesHere);
      // here use valueReturned some how.
    }
    Clear?

  11. #11
    grassclone is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: finding number of times a substring appears in a string recursively

    Quote Originally Posted by Fubarable View Post
    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.,

    Java 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

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: finding number of times a substring appears in a string recursively

    Java 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,389
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    prmdgurav is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Re: finding number of times a substring appears in a string recursively

    Quote Originally Posted by jim829 View Post
    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

Similar Threads

  1. Replies: 26
    Last Post: 12-03-2013, 02:55 PM
  2. Counting the length of a string recursively
    By Zoralink23 in forum New To Java
    Replies: 1
    Last Post: 11-29-2012, 03:37 AM
  3. Recursively searching a string
    By fam2315 in forum New To Java
    Replies: 0
    Last Post: 10-13-2012, 06:25 PM
  4. Dice help. posting the number of times a number is rolled.
    By cookiejarvus in forum New To Java
    Replies: 13
    Last Post: 12-04-2011, 11:08 PM
  5. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 10:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •