Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Using recursion to find the number of occurences of a character in a string

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Using recursion to find the number of occurences of a character in a string

    Having a bit of a problem with the recursive part of this case.

    here's what I have so far :
     public int freq (String S, Char C) {
    if (S.length () == 0) return (0); // base case
    else return // ....

    having trouble with the recursive case, I get that it will need to use the substring method, what i'm not sure of is if it needs a counter to count how man times the character occurs in the string. Any help appreciated.

  2. The Following User Says Thank You to sean1604 For This Useful Post:

    thomas10 (June 7th, 2013)


  3. #2
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Using recursion to find the number of occurences of a character in a string

    Then check to see if the first character of the string is the passed in character. If so, return one plus the results of the freq() with the first character removed from the string. Otherwise, just return the results of freq() with the first character removed from the string.

    public static int freq(String s, char c) {
        if (s.length() == 0)
            return 0;
        else if (s.charAt(0) == c)
            return 1 + freq(s.substring(1, s.length()), c);
        else
            return freq(s.substring(1, s.length()), c);
    }

    For example:

    If you call freq("literallyjer" 'l') then you would get 1 + 1 + 1 + 0 = 3.
    Last edited by literallyjer; October 22nd, 2009 at 06:12 PM.

  4. The Following 2 Users Say Thank You to literallyjer For This Useful Post:

    sean1604 (October 22nd, 2009), thomas10 (June 7th, 2013)

  5. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Using recursion to find the number of occurences of a character in a string

    thanks for the reply. I'm just starting out with recursion and was wondering if you could give me a bit more info on this part of the code:

    return 1 + freq(s.substring(1, s.length()), c);
    I get that recursive methods need to call themselves, i don;t understand how the first letter is removied in this case though and why you need the +1, thanks again.

  6. The Following User Says Thank You to sean1604 For This Useful Post:

    thomas10 (June 7th, 2013)

  7. #4
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Using recursion to find the number of occurences of a character in a string

    Look up the String.substring(int, int) method in the Java API.

    String s1 = "literally";
    String s2 = s1.substring(0, 7);
    System.out.println(s2) // prints: literal

    In our case, the substring method returns all the characters in the string starting from the second character (remember: counting indexes starts at 0) all the way the last character in the string.

    String s1 = "literally";
    String s2 = s1.substring(1, s1.length());
    System.out.println(s2) // prints: iterally

    Now, since our freq() method returns an integer, if the first character of the string is the same as our test character, then we want to return that integer plus one. If not, we just want to return whatever the next call to freq() returns.

    A simple example: freq("dad", 'd')

    freq is called with "dad" and 'd'
        is "dad" empty? no
        does the first letter in "dad" equal 'd'? yes
            add 1 to the next call to freq
            freq is called with "ad" and 'a'
                is "ad" empty? no
                does the first letter in "ad" equal 'd'? no
                freq is called with "d" and 'd'
                    is "d" empty? no
                    does the first letter of "d" equal 'd'? yes
                    add 1 to the next call of freq
                    freq is called with "" and 'd'
                         is "" empty? yes
                         return 0
                    return 1 + 0
           return 1 + 1 + 0
    return 1 + 1 + 0

    I hope that isn't too hard to follow.

  8. The Following User Says Thank You to literallyjer For This Useful Post:

    thomas10 (June 7th, 2013)

  9. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Using recursion to find the number of occurences of a character in a string

    What is you used s.isEmpty() instead of the first condition"(s.length() == 0)" will it be he same?

  10. The Following User Says Thank You to barqims For This Useful Post:

    thomas10 (June 7th, 2013)

  11. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Using recursion to find the number of occurences of a character in a string

    This thread is over 3 years old.
    Thread closed.
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    thomas10 (June 7th, 2013)

Similar Threads

  1. Problems with recursion
    By KingLane in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 20th, 2009, 11:02 PM
  2. [SOLVED] find the position of the field separator in the String---need help ASAP
    By rajesh.mv in forum Java Theory & Questions
    Replies: 6
    Last Post: August 17th, 2009, 10:33 AM
  3. [SOLVED] Getting Exception " java.util.InputMismatchException" in scanner package
    By luke in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 20th, 2009, 04:55 AM
  4. [SOLVED] How to string a decimal number in Java?
    By Lizard in forum Loops & Control Statements
    Replies: 6
    Last Post: May 14th, 2009, 03:59 PM
  5. How to read character from image area(jpg image)?
    By sundarjothi in forum Java Theory & Questions
    Replies: 5
    Last Post: August 6th, 2008, 02:08 AM